Link to home
Start Free TrialLog in
Avatar of Igor_Shtork
Igor_ShtorkFlag for Kazakhstan

asked on

Creating my DLL in Visual C++ and using in Visual Basic.

Hello!
How can I create my function in DLL file and then use it in my VB code?
So, for example. I want write very small function, which add constant (f.e. 5) to one variable. How can I call the function from Basic? How can I declare it in C++?
When I try do it by "MFC DLL wizard" (from first window of C++ environment), where should I add the declaration after?
Avatar of inpras
inpras

Hi
Create a MFC DLL project accept all defaults
let the dll name be MyDLL
in ur main cpp file add the declaration
extern "C" BOOL PASCAL EXPORT ExportedFunction();

for the function body
extern "C" BOOL PASCAL EXPORT ExportedFunction()
{
//If using any MFC
AFX_MANAGE_STATE(AfxGetStaticModuleState());
AfxMessageBox("Hi I am in DLL");

return TRUE;
}

in UR VB add the following code


Declare Function ExportedFunction Lib "MyDLL.dll" Alias "ExportedFunction" () As Long

U can call in UR module just by function name like this

Form_Load()
ExportedFunction();

End Sub
Hope this helps
inpras

Avatar of Igor_Shtork

ASKER

Thank you very much for Y answer, but:
1. I wrote your text in C++ and compile it fine, but when I try declare the function in Basic and inter your string I have this result:

Declare Function ExportedFunction Lib "Testdll.dll" () As Long

Why Basic delete: Alias "ExportedFunction" ?

2. Why you end of the declaration (in Basic) "as Long"? I think, that "as Boolean" will true? Or not?

3. When I try execute Basic's code I have the message: Can't find DLL entry point ExportedFunction in Mydll.dll. What does it mean?

You can declare function return value in as Boolean, Long, Integer - it doesn't matter.

If you need wery small function don't use MFC dll - use C++ dll.
Open Dll project and choose "A Dll that export some symbols"
The wisard give you some examples how to export classes, functions and variables.

Remove unnecessary - class and variable

Alias - so important?

Daniel
U can put it as BOOL no problem like this
Declare Function ExportedFunction Lib "MyDLL.dll" () As Boolean

U have to copy ur dll to UR windows directory

When the error is Cannot find dll entry point it means UR exe cannot find the dll U have to copy it to windows directory

Hope this helps
inpras
Hello all!

Thanks a lot for your help, but I can't solve my problem at the moment unfortunatuly. :-(( I have tried everything, you have told me, but there weren't any results.
I try to create a simple project in C++ by "Win32 Dynamic-Link Library" wizard, select "A Dll that export some symbols" in second window and compile it without any changes. The wizard put a simple example of exported function, which returns 42 as interger only.
So, I create a VB project and put second to description of Form:

Private Sub Form_Load()
Dim MyVar As Integer
MyVar = fnMyDll()
MsgBox MyVar, vbOKOnly
End Sub

and

Declare Function fnMyDll Lib "MyDll.dll" () As Integer

in additional module.

I put compiled MyDll.dll to current directory of VB project contaned Project.exe, to WINNT directory (I work under Windows NT 4.0 WS, is it possible is it important?), to WINNT/SYSTEM32 and to WINNT/SYSTEM. But when I try to run Project1.exe, I have a message "Can't find DLL entry point fnMyDll in MyDll.dll".

So, I understand nothing about!!! How is it possible??? Now, I'm sure about only one: the problem of VB only. I can say it because I tried to create the project of DLL in C++ by wizard only and it can't contane any mistakes.

Yours,
Igor Shtork

P.S. Thanks a lot for your information again, but I can't accept your answer because the problem isn't solved.
P.P.S. I thought, that I knew VB... It is a terrible day. :-((
ASKER CERTIFIED SOLUTION
Avatar of inpras
inpras

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Of couse! IgorS@terralink.almaty.kz

Yours,
Igor Shtork
Sorry, I forget write my ICQ too.
ICQ:23799113
I have sent the files

inpras
Thanks a lot, inpas! :-) I'll write a little post-question to you privatly.