Results 1 to 8 of 8

Thread: Creating a DLL in VC++

  1. #1
    Guest

    Talking

    Hi!
    I have Visual C++ 6.0 EE, and I am really bored, and I want to know how to make a DLL


    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.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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:

    [code]
    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.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The .DEF file needs to include the library name:
    Code:
    LIBRARY "mydll.dll"
    
    EXPORTS
        Message
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Guest
    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

  5. #5
    Guest
    sweeeeet! I got it to work!

    thanks a lot!!!!

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Guest
    I've got one more question...
    lets say I want to make a messagebox function

    Code:
    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...

  8. #8
    Guest
    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++?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width