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

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?
Igor_ShtorkIT expertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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


End Sub
Hope this helps

Igor_ShtorkIT expertAuthor Commented:
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?

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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
Igor_ShtorkIT expertAuthor Commented:
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


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.

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. :-((
Hi will U give me UR email id I will send both files with instructions

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Igor_ShtorkIT expertAuthor Commented:
Of couse!

Igor Shtork
Igor_ShtorkIT expertAuthor Commented:
Sorry, I forget write my ICQ too.
I have sent the files

Igor_ShtorkIT expertAuthor Commented:
Thanks a lot, inpas! :-) I'll write a little post-question to you privatly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.