CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    3

    compile c++ dll for visual basic

    HI, I have a previous c++ file that has all the code I need for my current project. Normally I would just rewrite it for visual basic but it requires tons of work arounds to complete. (The code involves nodes and pointers and tons of different stuff that visual basic doesn't fully support.) I understand there is a way to compile c++ code into a dll file though and call it in visual basic. I know you have to create a header file and a def file or something but I have no idea how about going about this. Any tutorials or sample code would be much appreciated.
    Thank You

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: compile c++ dll for visual basic

    You don't need a def file, you should be able to import the functions directly as long as you use __stdcall. I work with a guy who uses VB and I've written quite a few libraries for him. It doesn't even need to be Visual C++, I do it in MinGW.

  3. #3
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: compile c++ dll for visual basic

    Just export the required functions as __stdcall (or WINAPI) and you can very well use that C++ DLL in VB.
    Regards,
    Ramkrishna Pawar

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: compile c++ dll for visual basic

    and you can very well use that C++ DLL in VB
    Well, it will be so until it comes to exchanging strings, arrays, collections and other VB-specific stuff, when it immediately becomes a headache.
    Best regards,
    Igor

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: compile c++ dll for visual basic

    I understand there is a way to compile c++ code into a dll file though and call it in visual basic.
    Sure. The way is called ActiveX.
    Best regards,
    Igor

  6. #6
    Join Date
    Mar 2002
    Location
    Izhevsk, Udmurtia, Russia
    Posts
    930

    Re: compile c++ dll for visual basic

    moddll.cpp:
    Code:
    #define ABSTR BSTR
    
    char pText2[] = "0123456789 123456789 123456789 123456789";
    
    extern "C" __declspec(dllexport) ABSTR __stdcall PassString(LPSTR pText)
    {
    	int pTextLen = SysStringByteLen((ABSTR)pText);
    	strncpy(pText, pText2, pTextLen);
    	return SysAllocStringByteLen(pText2, strlen(pText2));
    }
    
    ABSTR __stdcall PassStringByDEFFile(LPSTR pText)
    {
    	int pTextLen = SysStringByteLen((ABSTR)pText);
    	strncpy(pText, pText2, pTextLen);
    	return SysAllocStringByteLen(pText2, strlen(pText2));
    }
    
    __declspec(dllexport) ABSTR __stdcall PassStringByCPP(LPSTR pText)
    {
    	int pTextLen = SysStringByteLen((ABSTR)pText);
    	strncpy(pText, pText2, pTextLen);
    	return SysAllocStringByteLen(pText2, strlen(pText2));
    }
    
    BOOL APIENTRY DllMain( HANDLE hModule, ...
    moddll.def:
    Code:
    ; moddll.def : Declares the module parameters.
    
    LIBRARY      "moddll.DLL"
    
    EXPORTS
    	DllMain     @1 PRIVATE
      PassStringByDEFFile
    basmoddll.bas:
    Code:
    Private Declare Function PassString Lib ".\moddll.dll" Alias "_PassString@4" (ByVal s As String) As String
    
    Private Declare Function PassStringByDEFFile Lib ".\moddll.dll" (ByVal s As String) As String
    
    Private Declare Function PassStringByCPP Lib ".\moddll.dll" Alias "?PassStringByCPP@@YGPAGPAD@Z" (ByVal s As String) As String
    
    Sub Main()
      Dim s As String, s1 As String
    
      s = "xxxxx"
      s1 = PassString(s)
      
      s = "xxx"
      s1 = PassStringByDEFFile(s)
      
      s = "ZZZZZZZ"
      s1 = PassStringByCPP(s)
      
    End Sub
    With best wishes,
    Vita
    -----------------------
    Russian Software Development Network -- http://www.rsdn.ru

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured