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?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

inprasConnect With a Mentor Commented:
Hi will U give me UR email id I will send both files with instructions
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_ShtorkAuthor 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?
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


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?

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_ShtorkAuthor 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. :-((
Igor_ShtorkAuthor Commented:
Of couse!

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

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

From novice to tech pro — start learning today.