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

    Calling a C DLL routine from Visual Basic


    Hi,

    I have a C DLL routine which looks like this:

    const char* C_routine(const char* msg, int length, const char* msg2);




    How to call this routine from VB?
    here is what i have done

    Private Declare Function MyC Lib "test.dll" (ByVal msg As String, ByVal length As Integer, ByVal msg2 As String) As String

    Private Sub Class_Initialize()
    Dim test As String

    test$ = C_routine("message", 7, "msg2") '//error message at this line!! Bad DLL calling convention

    End Sub



    The C_routine is executed as it supposed to, but at the end of execution, i got an error message saying "Bad DLL calling convention"!! what have i done wrong? Pls help.. Does the C_routine need to modify the parameters type in order to match VB type? if so, how??


  2. #2
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Calling a C DLL routine from Visual Basic

    Your C dll should be declared with _stdcall calling convention.

    _stdcall const char* C_routine(const char* msg, int length, const char* msg2);
    Even this will not WORK, because there is no char * equivalent in VB.

    please remember that this kind of char * return may give troubles with VB as there are differences in the memory handling between the two. and if you dont free the memory then memory leaks/lockup.

    Better would be typical windows way: pass in a allocated string , and also tell the size, the routine will fill in the buffer not exceeding the specified size: offhand example is GetPrivateProfileString fn.


    /* Retuns the no. of chars filled in the buffer */
    _stdcall long C_routine(const char* msg, int length, const char* msg2 , char * lpBuf, long buflen);

    private Declare Function MyC Lib "test.dll" Alias "C_routine" (byval msg as string, byval length as Integer, byval msg2 as string, byval lpBuf as string , byval BufLen as Long ) as Long

    private Sub Class_Initialize()
    Dim test as string
    'test$ = C_routine("message", 7, "msg2") '//error message at this line!! Bad DLL calling convention

    test = space(256)
    rtn = MyC("message",7,"msg2",test,256)
    if rtn > 0 then
    test = left$(Test,rtn)
    end if
    End Sub



    Also you should define the C_Routine in a .Def file, otherwise you will get a "Dll Function not found" error


    RK

  3. #3
    Join Date
    Jul 1999
    Location
    Israel
    Posts
    140

    Re: Calling a C DLL routine from Visual Basic

    You have to recieve the string as a BSTR.

    const char* C_routine(BSTR msg, int length, BSTR msg2);

    then in order to get char*
    you have to declare
    USES_CONVENSION in the begining of the routine and
    char charMsg[256];
    strcpy(charMsg, OLE2A(msg)







  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Calling a C DLL routine from Visual Basic

    just one more comment:
    a C "int" is a "Long" in VB!
    Even in 32 bit VB versions, an "Integer" is only 2 Bytes, but a C int is 4 bytes.


  5. #5
    Join Date
    Mar 2000
    Location
    Germany
    Posts
    13

    Re: Calling a C DLL routine from Visual Basic

    If you want to use LPCTSTR in your C/C++ code insert the following code in your stdafx.h
    #ifndef UNICODE
    #define UNICODE
    #endif
    #ifndef _UNICODE
    #define _UNICODE
    #endif

    this code applies to MS VC++ ver 5.0 and higher


  6. #6
    Join Date
    Sep 1999
    Location
    Ohio
    Posts
    33

    Re: Calling a C DLL routine from Visual Basic

    Here is the code for an .DLL I wrote to try and figure things out:


    #include <windows.h>

    void __stdcall GetMyComputerName( LPSTR szComputerName, LPSTR szErrorMsg, HWND hWnd )
    {
    MessageBox( hWnd, szComputerName, szErrorMsg, MB_OK );
    }




    It works fine in VB. Here is how I declared it in VB:


    Declare Sub GetMyComputerName Lib "vb_dll.dll" (byval ComputerName as string, byval strErrorMsg as string, byval window as Long)




    It works for me.


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