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

    Calling VB DLL from C

    Hi..i'm writing an application in VC env and it's is the Win32 console application type.One of the function of the appliction is to call DLL in VIsual BAsiC. Is it possible for me to call the dll from my application? if it is, how it can be done?


  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Calling VB DLL from C

    A dll written in Visual Basic is a COM (ActiveX) dll and can be accessed in exactly the same way as any non-VB COM dll - through its interface.

    AFAIK you cannot bypass the interface (and I see no reason why you would).

    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  3. #3
    Join Date
    Nov 1999
    Posts
    9

    Re: Calling VB DLL from C

    How it can be done ? What should i write in my C code in orderto call the DLL? i found teh sample..but it;s in C++....using #import ...but if it in C, can i do it the same way???


  4. #4
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Calling VB DLL from C

    No - #import is static linking - which absolutely cannot be done with a VB as a target.
    What you need is to use the functions exported by "OLEAUT.DLL" and the IDispatch interface (IDispatch::QueryInterface) etc.
    However - there are a large number of tricky parts to this - so I'm going to have to recommend a book....or several hours on msdn.microsoft.com.

    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  5. #5
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: Calling VB DLL from C

    Can you use "LoadLibrary()" in C? (can't remember the exact function name)


  6. #6
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Calling VB DLL from C

    yes...but LoadLibrary() is for standard (non COM) dlls.
    What you need to do is load the dll using "CoCreateInstance()" which takes the GUID of the OLE dll which you can be discovered using the RegEdit application.
    This will return an instance handle which you can then pass to "QueryInterface()" and when you are done with the dll, use "Release()" to let COM know you no longer need the dll.


    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  7. #7
    Join Date
    Nov 1999
    Posts
    9

    Re: Calling VB DLL from C

    well, i have done a sample program using #import to call the VB DLL and it works. Can you explain to me what do you mean by static linking if we use the #import to call the DLL? Am I doing it the right way??TQ

    Below are the codes on how i call the DLL from my C console application.
    ++++++++++++++++++++++++++++++++++++++++++
    include &lt;stdio.h&gt;
    #include &lt;windows.h&gt;
    #include &lt;stdlib.h&gt;
    #import "f:\IVR\IVR program\convert1.dll" no_namespace


    void main()

    {

    //initialize variable for bstr
    BSTR bstrDesc;
    BSTR st1;
    char bufin[10];//input variable
    char bufout[10];//output variable
    BSTR buffinbstr = NULL;
    WCHAR buffer1[512];



    try

    {
    CoInitialize(NULL);

    //Request input to be send
    printf("Enter string");
    scanf("%s",&bufin);

    //Tukar ANSI to UNICODE
    MultiByteToWideChar(CP_ACP,0,bufin,-1,buffer1,512);
    buffinbstr = SysAllocString(buffer1);




    _convert1Ptr ptr;

    ptr.CreateInstance(_uuidof(convert1));

    st1 =ptr-&gt;ConvertUpper(buffinbstr);
    //st1 = st;

    //Tukar UNICODE to ANSI
    WideCharToMultiByte(CP_ACP,0,st1,-1,bufout,80,NULL,NULL);

    //print output frm VB
    printf("sucess output ,%s \n",&bufout);

    }

    catch(_com_error &e)
    {
    bstrDesc = e.Description();

    }



    SysFreeString(buffinbstr);


    CoUninitialize();
    }






  8. #8
    Join Date
    Nov 1999
    Posts
    9

    Re: Calling VB DLL from C

    well, i have done a sample program using #import to call the VB DLL and it works. Can you explain to me what do you mean by static linking if we use the #import to call the DLL? Am I doing it the right way??TQ

    Below are the codes on how i call the DLL from my C console application.
    ++++++++++++++++++++++++++++++++++++++++++
    include &lt;stdio.h&gt;
    #include &lt;windows.h&gt;
    #include &lt;stdlib.h&gt;
    #import "f:\IVR\IVR program\convert1.dll" no_namespace


    void main()

    {

    //initialize variable for bstr
    BSTR bstrDesc;
    BSTR st1;
    char bufin[10];//input variable
    char bufout[10];//output variable
    BSTR buffinbstr = NULL;
    WCHAR buffer1[512];



    try

    {
    CoInitialize(NULL);

    //Request input to be send
    printf("Enter string");
    scanf("%s",&bufin);

    //Tukar ANSI to UNICODE
    MultiByteToWideChar(CP_ACP,0,bufin,-1,buffer1,512);
    buffinbstr = SysAllocString(buffer1);




    _convert1Ptr ptr;

    ptr.CreateInstance(_uuidof(convert1));

    st1 =ptr-&gt;ConvertUpper(buffinbstr);
    //st1 = st;

    //Tukar UNICODE to ANSI
    WideCharToMultiByte(CP_ACP,0,st1,-1,bufout,80,NULL,NULL);

    //print output frm VB
    printf("sucess output ,%s \n",&bufout);

    }

    catch(_com_error &e)
    {
    bstrDesc = e.Description();

    }



    SysFreeString(buffinbstr);


    CoUninitialize();
    }










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

    Re: Calling VB DLL from C

    just a comment: #import has absolutely nothing to do with static linking.
    From the docs:
    "The #import directive is used to incorporate information from a type library. The content of the type library is converted into C++ classes, mostly describing the COM interfaces."

    #import creates C++ code for calling the COM interfaces. No linking at all.
    And yes, you can import VB Activex dlls.
    The easiest way to do that (if you are looking for an easy way), write an MFC application and use ClassWizard to import the VB ActiveX Dll. You can use MFC even in a console app.



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