Hi! I'm Newbie in programming and I'm interested in VB and VC++.My problem is that I don't know completely how to program VB or VC++ Dll so that i will NOT repeat procedures in each exe i write. Is there anybody to guide me to write simple example?


With regards, EvStevemd
------Ask to Gain Understanding------------

Dani AI

Generated

A short, practical primer for based on 's hint: there are three common, reliable ways to reuse VC++ code from Visual Basic. Choice depends on which VB is targeted (classic VB6 vs VB.NET) and whether a simple function API or true object semantics are needed.

For simple, procedural calls (easy for beginners): build a native DLL that exports plain C functions. Prevent C++ name-mangling with extern "C", pick a calling convention VB expects (usually __stdcall), and export with __declspec(dllexport). Example native function:

extern "C" __declspec(dllexport) int __stdcall Add(int a, int b)
{
    return a + b;
}

Declare it in VB6 like this:

Declare Function Add Lib "MyNative.dll" (ByVal a As Long, ByVal b As Long) As Long

For VB.NET, either invoke the same native DLL with P/Invoke (DllImport) or wrap native code in a managed C++/CLI layer and expose .NET classes. Simple P/Invoke example:

<DllImport("MyNative.dll", CallingConvention:=CallingConvention.StdCall)>
Public Shared Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
End Function

When object orientation is required, create a COM/ActiveX object (callable from VB6 and VB.NET via COM interop) or write a C++/CLI wrapper that translates native classes into managed types.

Practical gotchas and tips:

  • Use extern "C" and explicit calling convention to avoid name-mangling and mismatches.
  • Match bitness: VB6 is 32-bit; the DLL must be 32-bit. For VB.NET match the process bitness.
  • Keep exported APIs simple (ints, doubles). Strings and pointers need careful marshaling or clear ownership conventions.
  • Tools: dumpbin /exports or Dependency Walker to inspect exports; enable debug symbols for easier troubleshooting.

Further reference: Microsoft guidance on exporting native APIs (Exporting from a DLL), platform invoke (P/Invoke best practices), and COM interoperability ().

Recommended Answers

All 3 Replies

See And here are some google links you might also want to read. Looks like some of them have example code.

Thanks A.dragon I will do it and for any Question,i'll post.
-------Good Luck-----

non-static C++ class code can not be called from VB so you have to write the DLL as C code or as static methods in C++. In both cases the functions have to be exported when compiling the DLL.

In VB code just predeclare the functions as you would any other DLL function such as win32 api functions. I don't recall the exact syntax but there must be hundreds of examples.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.