|
-
February 14th, 2000, 08:09 AM
#1
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?
-
February 14th, 2000, 09:19 AM
#2
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).
-
February 15th, 2000, 11:52 AM
#3
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???
-
February 15th, 2000, 12:29 PM
#4
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.
-
February 15th, 2000, 02:42 PM
#5
Re: Calling VB DLL from C
Can you use "LoadLibrary()" in C? (can't remember the exact function name)
-
February 16th, 2000, 04:17 AM
#6
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.
-
February 23rd, 2000, 08:15 PM
#7
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 <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();
}
-
February 27th, 2000, 10:54 PM
#8
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 <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();
}
-
February 28th, 2000, 02:54 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|