Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Internet & the Web (https://www.askmehelpdesk.com/forumdisplay.php?f=177)
-   -   Accessing VB dll from VC++ (https://www.askmehelpdesk.com/showthread.php?t=18734)

  • Jan 25, 2006, 06:19 AM
    john_christopher_s
    Accessing VB dll from VC++
    I have developed windows dll from VB. (The site http://www.windowsdevcenter.com/pub/...ll.html?page=3 gives a way to generate windows dll from VB). When I try to access it from VC++, it gives run time error as access violation. But I can access it from VB client

    If anybody knows the solution to access the DLL in VC++ , created from VB please let me know

    Thanks
  • Jan 28, 2006, 11:10 PM
    Ademan
    Well, I figure there's got to be plenty at Google, but regardless I'll give my best educated guess. But first you need to help us all out. How are you linking with the dll? If you're using some built in IDE functionality, you may need to link with it dynamically. If you're using a static lib (that contains all of the symbols for the dll) then you again, might want to try dynamic linking. Finnaly, depending on the version of vb (then again maybe not) there very well may be a dll that you need to link with to allow vb dlls/apps to work (I know there is for at least the older versions of vb, I don't know about newer though) also make sure you've created a win32 program not a win32 console program. The win32 console program PROBABLY won't link with all the necessary libraries to make the win32 API work

    Hope that helps
    -Dan

    EDIT: ah yes, also beware what directory your dll is in, it needs to be either in the windows/system32 folder, or in the program's directory. Now be careful though, msvc has been considered 'wierd' by many, because In DEBUG mode the program's directory is actually the project directory, as opposed to the project directory/DEBUG
  • Jan 29, 2006, 09:20 PM
    john_christopher_s
    Thanks for your kind reply. Actually I have an executable, which is created in VC++. (I don't know what kind this source is , i.e. win32 console or win32 program or any other). This program will reads dll and do the operations on the particular functions, (the function names they gave). If we create dll from vc++ then it works fine, but if we create from VB it tells access violation error.

    But Loadlibrary works and getprocaddress also works fine. While calling function it tells error. Please if you have any example program help to me.

    Thanks,
  • Jan 29, 2006, 09:54 PM
    Ademan
    Hrm, well if load library doesn't result in any sort of error then I don't know what to say. LoadLibrary returns NULL if it fails, I don't KNOW if you're checking for that or not, but you should be (and it seems you are). However, I know at least in the case of C++ if a function belongs to a class, and you export that function, when you use GetProcAddress() you don't use the string you would expect, as opposed to say "CFoo::Bar()" it would be something like "ZCFOO@@!BarV" (of course that's entirely hypothetical) but that's referred to as "name mangling" and its how c++ deals with overloaded functions and classes being exported to dlls. I would assume (since to the best of my knowledge VB supports operator overloading) that VB uses name mangling as well.

    I'm sorry I'm not of more help. Anyway here are a few links that might help

    http://www.daniweb.com/techtalkforums/thread17300.html


    http://support.microsoft.com/default.aspx?kbid=194873

    Hope that helps
    -Dan
  • Jan 31, 2006, 01:36 AM
    john_christopher_s
    Thanks for your help. I have checked the two dlls (from VC++ and VB). I got the same function names. While in VB it gives additional four functions (like DllRegisterServer). And Im not at all able to call that function from VC++. Actually LoadLibrary loads that dll and getProcAddress takes the corresponding function. (I have debugged it). But when I try to call that function it says access violation error.
    I have debugged it. The control will be passed to the corresponding function, inside that function only I got that error, if I call that same function in VB client I didn't get that error. It works fine for VB client.
    I have used only generic data types like integer.
    If you got any idea please share with me

    Thank you,
  • Jan 31, 2006, 10:20 AM
    Ademan
    Yeah it sounds to me like the VBdll has an additional dependency, like ActiveX or something, I really don't know enough about how VB Dlls work. But if GetProcAddress returns valid addresses and the access violation occurrs INSIDE the function that you call. I'm willing to bet money it has to do with an external dependency.

    Cheers
    -Dan
  • Feb 10, 2006, 03:39 AM
    esteve
    Quote:

    Originally Posted by john_christopher_s
    I have developped windows dll from VB ... when I try to access it from VC++, it gives run time error as access violation.

    Hi,

    Here is the solution:
    Create the dll in VB (as article describes it), function header like this:
    Code:

    Public Function example_function1(ByVal i As Integer) as Integer
        example_function = i
    End Function

    Public Function example_function2(ByRef i As Integer) as Integer
        example_function = i
        i = i * 2
    End Function

    In C++:
    Code:

    typedef int (__stdcall *ptf_test_func_1_type)(int);
    typedef int (__stdcall *ptf_test_func_2_type)(int*);

    LoadLibrary(...);
    //check its result...
    ptf_test_func_1_type p_func1=(ptf_test_func_1_type)GetProcAddress(dll_instance,"example_function1");
    ptf_test_func_2_type p_func2=(ptf_test_func_2_type)GetProcAddress(dll_instance,"example_function2");
    //check its results...

    //call function:
    int i =1;
    int ret_val = (*p_func1)(i);
    int ret_val2 = (*p_func2)(&i);

    So simple.
    Troubles begin at strings... (In VB: "As String", in c++: "BSTR")

    Good luck,
    Istvan

  • All times are GMT -7. The time now is 04:52 PM.