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?
Printable View
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?
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).
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???
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.
Can you use "LoadLibrary()" in C? (can't remember the exact function name)
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.
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 <stdio.h>
#include <windows.h>
#include <stdlib.h>
#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->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();
}
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 <stdio.h>
#include <windows.h>
#include <stdlib.h>
#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->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();
}
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.