really, really basic DLL use

This is a discussion on really, really basic DLL use within the Windows Programming forums, part of the Platform Specific Boards category; Heya, I'm a mostly UNIX C++ programmer who is trying to transition into Windows programming, and I would like to ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    82

    really, really basic DLL use

    Heya, I'm a mostly UNIX C++ programmer who is trying to transition into Windows programming, and I would like to know how to create and call a DLL. I am used to the "compile as a shared library, declare as extern, and pass the library's name to the linker through the command line" paradigm. I have examined numerous Windows tutorials that don't address the starting points. My question (and please point me to a tutorial that covers this if you know of one):

    1. What special commands do I put into a program if I want to compile it into a DLL?
    2. What special commands to I put into the program that calls the function in the DLL?
    3. What kind of compiler/linker options do I need to use, specifically for Borland command-line tools?

    If it would make it easier, lets use an example:

    Let's say I want to compile the following program into a DLL:
    Code:
    #include <iostream>
    
    void hello() {
        std::cout << "Hi there!" << endl;
    }
    Normally I would use the following syntax in the calling program:
    Code:
    extern void hello();
    int main() {
        hello();
    }
    Then I would compile the first code with the -G command in the CC compiler to create a dynamically shared library, and compile the second with the -l command which tells the linker to look in the hello library for the hello function. So, if you please, take these code snippets and add to them the necessary verbiage for DLLs.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here is the code you need infront of the source code to export a DLL.

    Code:
    extern "C" __declspec(dllexport) void hello()
    {
        std::cout << "Hi there!" << endl;
    }
    Kuphryn

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by kuphryn
    Here is the code you need infront of the source code to export a DLL.

    Code:
    extern "C" __declspec(dllexport) void hello()
    {
        std::cout << "Hi there!" << endl;
    }
    Kuphryn
    That may work in Borland...but I'm not sure......

    If Borland doesnt support __declspec(dllexport) then you have to use a .DEF file in the dll project to tell the linker what to export.....bets bet is to look at the documentation for your compiler & linker...

    BTW, Kuph.....managed to do a dll like that in MASM yet?

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Fordy,

    I wish!!! It's a dream.

    hahahahah

    Kuphryn

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Okay, please forgive my density:

    I have the following:
    Code:
    #include <iostream>
    
    extern "C" __declspec(dllexport) void hello() {
        std::cout << "Hi there!" << std::endl;
    }
    saved as hello.cpp. I compiled this with:

    bcc32 -WD hello.cpp

    which generated hello.dll, hello.obj, and hello.tds. I then have:
    Code:
    extern "C" __declspec(dllexport) void hello();
    
    int main() {
        hello();
    }
    which is saved as test.cpp. I try to compile with
    bcc32 -L. test.cpp
    with the -L. flag set to add the current directory to the linker's search path. I receive the following linker error:

    Error: Unresolved external '_hello' referenced from E:\CODE\CPP\WINTEST\TEST.OBJ

    Obviously it can't find the right path, which means that I have left out a critical step somewhere. I have looked all through the linker options and seen nothing that says to me "use this option to specifically link to this DLL". I realize that this is simple stuff for y'all, but please bear with me.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    When you create the dll, there should be a .lib file created...this need to be used with your main program

    Kuph:

    To whet your appitite;

    KuphDll.asm
    Code:
    .386
    .model flat,stdcall
    option casemap:none
    include \masm32\include\windows.inc
    include \masm32\include\user32.inc
    includelib \masm32\lib\user32.lib          
    include \masm32\include\kernel32.inc
    includelib \masm32\lib\kernel32.lib
    
    hello proto 
    DllEntry proto hInstDLL: HINSTANCE, reason: DWORD, reserved1: DWORD
    
    
    
    .DATA 
    
    MyStr BYTE "Hi Kuphryn!",'0'
    hCon HANDLE NULL
    dwDummy DWORD NULL
    
    ;#####################################################
    ;#####################################################
    
    
    ; STARTUP
    
    .CODE              
    
    DllEntry proc hInstDLL: HINSTANCE, reason: DWORD, reserved1: DWORD
           mov eax,TRUE
           ret
    DllEntry Endp
    
    
    hello proc 
    	invoke GetStdHandle,STD_OUTPUT_HANDLE
    	mov hCon,eax
    	invoke WriteConsole,hCon,ADDR MyStr,11,ADDR dwDummy,NULL
    	ret
    hello endp
    
    end DllEntry
    KuphDll.def
    Code:
    LIBRARY KuphDll
    EXPORTS hello
    Build.bat
    Code:
    'Change these paths if needed
    C:\masm32\BIN\ML.EXE /c /coff /Cp KuphDll.asm
    C:\masm32\BIN\LINK.EXE /DLL /DEF:KuphDll.def /SUBSYSTEM:WINDOWS /LIBPATH:c:\masm32\lib KuphDll
    PAUSE
    Now that should produce a dll and a lib....

    Now...Kuph.Cpp
    Code:
    extern "C" void __stdcall hello(void);
    
    int main(void){
    
    	hello();
    	return 0;
    }
    Make sure you include the lib in the cpp project and have the dll in the same dir as the exe.........

    Gotta love MASM......all the cool, but stripped to the flesh!!!!!

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Just beautiful!

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. Replies: 3
    Last Post: 07-19-2008, 03:12 PM
  3. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21