PDA

Click to See Complete Forum and Search --> : Creating a DLL in VC++


Dec 22nd, 2000, 12:16 AM
Hi!
I have Visual C++ 6.0 EE, and I am really bored, and I want to know how to make a DLL :rolleyes:


I know that you have to select "Dll" from the project menu...

but are there any special functions I have to write at the top(like WinMain, in a windows app?)...

I once tried to make a DLL, and when I tried it in VB it said "no dll entry point"

all I had was a function that called MessageBox()




Thanks,
Dennis

PS: I have looked in MSDN and I cannot find any good examples.

Vlatko
Dec 22nd, 2000, 05:06 AM
Dll's can have a function like WinMain in this case DLLMAin
which you use if you want to do something when the dll is loaded or unloaded in memory:

BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved // reserved
);

Parameters
hinstDLL
A handle to the DLL. The value is the base address of the DLL. The HINSTANCE of a DLL is the same as the HMODULE of the DLL, so hinstDLL can be used in subsequent calls to the GetModuleFileName function and other functions that require a module handle.
fdwReason
Specifies a flag indicating why the DLL entry-point function is being called. This parameter can be one of the following values: Value Meaning

DLL_PROCESS_ATTACH Indicates that the DLL is being loaded into the virtual address space of the current process as a result of the process starting up or as a result of a call to LoadLibrary. DLLs can use this opportunity to initialize any instance data or to use the TlsAlloc function to allocate a thread local storage (TLS) index.
DLL_THREAD_ATTACH Indicates that the current process is creating a new thread. When this occurs, the system calls the entry-point function of all DLLs currently attached to the process. The call is made in the context of the new thread. DLLs can use this opportunity to initialize a TLS slot for the thread. A thread calling the DLL entry-point function with DLL_PROCESS_ATTACH does not call the DLL entry-point function with DLL_THREAD_ATTACH.
Note that a DLL's entry-point function is called with this value only by threads created after the DLL is loaded by the process. When a DLL is loaded using LoadLibrary, existing threads do not call the entry-point function of the newly loaded DLL.

DLL_THREAD_DETACH Indicates that a thread is exiting cleanly. If the DLL has stored a pointer to allocated memory in a TLS slot, it uses this opportunity to free the memory. The system calls the entry-point function of all currently loaded DLLs with this value. The call is made in the context of the exiting thread.
DLL_PROCESS_DETACH Indicates that the DLL is being unloaded from the virtual address space of the calling process as a result of either a process exit or a call to FreeLibrary. The DLL can use this opportunity to call the TlsFree function to free any TLS indices allocated by using TlsAlloc and to free any thread local data.


lpvReserved
Specifies further aspects of DLL initialization and cleanup.
If fdwReason is DLL_PROCESS_ATTACH, lpvReserved is NULL for dynamic loads and non-NULL for static loads.

If fdwReason is DLL_PROCESS_DETACH, lpvReserved is NULL if DllMain has been called by using FreeLibrary and non-NULL if DllMain has been called during process termination.

Return Values
When the system calls the DllMain function with the DLL_PROCESS_ATTACH value, the function returns TRUE if it succeeds or FALSE if initialization fails. If the return value is FALSE when DllMain is called because the process uses the LoadLibrary function, LoadLibrary returns NULL. If the return value is FALSE when DllMain is called during process initialization, the process terminates with an error. To get extended error information, call GetLastError.

When the system calls the DllMain function with any value other than DLL_PROCESS_ATTACH, the return value is ignored.


If you put this in a C++ dll:


void _stdcall message(HWND hwnd)
{
MessageBox(hwnd,"text","Text",MB_OK);
}

You can also use a def file and write the function names you want to export there

EXPORTS
Message

To use it in vb:
[code]
Private Declare Sub Message Lib "dllname.dll" (ByVal hwnd as Long)

'call it
Message Form1.hWnd
'pops up a message box


The dll is only functions which you call. VB apps can be much much faster when you use functions from a C++ DLL.

parksie
Dec 22nd, 2000, 06:30 AM
The .DEF file needs to include the library name:

LIBRARY "mydll.dll"

EXPORTS
Message

Dec 22nd, 2000, 10:40 AM
I am not planning on using them with VB... well, maybe putting together a library for other people... :)

but... why the underscore before the function name?

I read in one of your previous posts that that said that it exported it or something??

if it does that. do you need the .def file?


thanks,
Dennis

Dec 22nd, 2000, 11:01 AM
sweeeeet! I got it to work!

thanks a lot!!!! :D

parksie
Dec 22nd, 2000, 12:08 PM
Well, in C all functions are prepended with an underscore. C++ uses different "name mangling" (honestly!) semantics.

So, in C++ to get the C name use extern "C" function which goes to _function.

Dec 24th, 2000, 06:11 PM
I've got one more question...
lets say I want to make a messagebox function


void _stdcall message(HWND hwnd, ??? text)
{
MessageBox(hwnd,text,"Text",MB_OK);
}




do I use:
char *
LPCSTR
string (from STL string lib)


???


and one more thing.. instead of

void _stdcall message(HWND hwnd)


can I use
void WINEXPORT message(HWND hwnd)


?
I read somewhere that I could...

Dec 24th, 2000, 06:16 PM
oh, and the last question.....

how do I use my DLL in C++?
I know how in VB, but how do I do it in C++?