|
-
October 30th, 2001, 11:25 PM
#1
Simple DLL
Hi All,
Now I try to learn how to write a simple DLL to export some API functions. Do any body help me to do this?
Thanks so much
Khin Lau
-
October 31st, 2001, 12:16 AM
#2
Re: Simple DLL
Take a look at the following code
// DLL1.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
// if forgot to give the Linker switch
#pragma comment(linker, "/DLL")
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
__declspec (dllexport) int __stdcall Sum(int p_no1, int p_no2)
{
return p_no1 + p_no2;
}
Hope it helps.
-
November 7th, 2001, 11:15 PM
#3
Re: Simple DLL
Hi,
I have built your code to DLL1.dll, it is all OK
but when I use Sum function in VB it have an error.
My VB code like this:
Private Declare Function Sum Lib "DLL1.DLL" (Byval p_no1 As Integer,Byval p_no2 As Integer) As Integer
Private Sub Command1_Click()
MsgBox Sum(1, 1)
End Sub
and the Error show: Can't find entry point Sum in DLL1.dll
Can you tell me why?
Thank you very much.
-
November 8th, 2001, 12:28 AM
#4
Re: Simple DLL
use
extern "C"
before function name to supress decoration.
// DLL1.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
// if forgot to give the Linker switch
#pragma comment(linker, "/DLL")
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
extern "C" __declspec (dllexport) int __stdcall Sum(int p_no1, int p_no2)
{
return p_no1 + p_no2;
}
Hope it helps.
-
November 8th, 2001, 02:04 AM
#5
Re: Simple DLL
I do all as you, but still have error.
I don't know why, in my VB program still show: can't find Dll entry point Sum in DLL1.dll.
Do you have any ideas?
-
November 28th, 2001, 12:02 PM
#6
Re: Simple DLL
The reason that you can not find the Dll entry point is because the __STDCALL keyword changes the name of the dll entry point function. It prefixes the char "_" to the front of the function name and postfix a string "@XX" where XX is the number of bytes in the argument list.
Hope this helps.
-
November 28th, 2001, 07:55 PM
#7
Re: Simple DLL
Can you explain more? I still don't understand how can I use Sum function.
Thanks
-
November 29th, 2001, 12:35 AM
#8
Re: Simple DLL
If you want to get the sample above working with VB you must use a DEF file in the C/C++ project. In the DEF file you will be explicitly exporting the DLL entry function names. Apparently this is the way you need to do it when using VB.
Here is following is a working version of the sample.
mydll.cpp file
// if forgot to give the Linker switch
//#pragma comment(linker, "/DLL")
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
return TRUE;
}
int __stdcall Sum(int p_no1, int p_no2)
{
return p_no1 + p_no2;
}
mydll.def file
LIBRARY mydll
EXPORTS
Sum @1
Notice in the code above there is no extern "C" and there is a __stdcall. What does this mean? Because we specified __stdcall the DLL entry point name would be mangled. It will become _Sum@8. Why do we need __stdcall? It keeps the call stack in order and keeps VB happy. So how do we get around this? This is where the def file comes into play. The def file will force the DLL entry _Sum@8 to Sum eliminating the name mangling.
Are you still with me? Good, now all we need to do is write the VB code that will utilize the DLL.
VB Code
Option Explicit
Private Declare Function Sum Lib "E:\WorkSpace\C++\DllEntry\Debug\mydll" (ByVal x As Integer, ByVal y As Integer) As Integer
Private Sub Form_Activate()
MsgBox Sum(100, 100)
End Sub
A word of warning, I just got this working in the morning and I am not claiming to be an expert on this, but I did get this example to work for me and a more complicated example also.
A very useful utility is “Depends.exe” which is disturbed with MS Visual studio it allow one to see the dependent DLLs and DLL entry points. Another utility is Dumpbin.exe this link has more information on it http://msdn.microsoft.com/library/de...ated_names.asp
If you want the project files post your email address and I will email them to you.
Hope this helps,
Jamel
Windows 2000 MCSE
MCP
-
December 1st, 2001, 07:24 AM
#9
Re: Simple DLL
how can i do the same thing just from VC ?
i meen i can i load this dll from VC
i try with loadlibrary and getprocaddress but i got an runtime error!
-
December 13th, 2001, 12:36 PM
#10
Re: Simple DLL
This post explaining the DEF file for all .dll files was so helpful that I finally created a CodeGuru username just so that I could give my thanks and point out how great this was. I've searched the web for the answer to errors like:
error LNK2001: unresolved external symbol "__declspec(dllimport)
and for how to link a .dll to a VB program and this was the only thing that helped. Thanks Jamel, keep enlightening people.
/*******************************************\
"This, this is ice. This is what happens to water when it freezes. This, this is Kent. This is what happens to people when they get too sexually frusterated."
- Chris Knight
\*******************************************/
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
|