compile as .dll

This is a discussion on compile as .dll within the C++ Programming forums, part of the General Programming Boards category; If i have a program, in order to compile it as a dll there isnt just a .dll button? im ...

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    compile as .dll

    If i have a program, in order to compile it as a dll there isnt just a .dll button?

    im using microsoft visual c++, but i was reading a tutorial on how to do it and it sounds much harder than expected. is this true?

    http://www.codeguru.com/forum/showthread.php?t=231254

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Dll-handling on windows can be a pain in the ass in some cases compared to linux. But the description is only about the standard case and should work straight forward if you do what they tell. Post some code and mark what you find to hard, then one can help you out.

  3. #3
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    OK i cant get this to work here is my code:

    Code:
    #include "stdafx.h"
    #include "AlazarApi.h"
    #include "AlazarCmd.h"
    #include "windows.h"
    #include "stdio.h"
    #include "conio.h"
    
    int __declspec(dllexport) __stdcall TestDLL(const char FileNameA[1024], 
    											const char FileNameB[1024]){
    	oldmain();
    	return 1;
    }
    Code:
    int oldmain()
    {
        ***
    }
    Here are my errors:

    Code:
    Compiling...
    TestDLL.cpp
    C:\AlazarTech\ATS-SDK\V5_2_0\ATS460-SDK\Development\Beta\Acq2DiskDLL\TestDLL\TestDLL.cpp(13) : error C2065: 'oldmain' : undeclared identifier
    C:\AlazarTech\ATS-SDK\V5_2_0\ATS460-SDK\Development\Beta\Acq2DiskDLL\TestDLL\TestDLL.cpp(115) : error C2373: 'oldmain' : redefinition; different type modifiers
    C:\AlazarTech\ATS-SDK\V5_2_0\ATS460-SDK\Development\Beta\Acq2DiskDLL\TestDLL\TestDLL.cpp(189) : error C2065: 'malloc' : undeclared identifier
    C:\AlazarTech\ATS-SDK\V5_2_0\ATS460-SDK\Development\Beta\Acq2DiskDLL\TestDLL\TestDLL.cpp(192) : error C2065: 'free' : undeclared identifier
    Error executing cl.exe.
    
    TestDLL.obj - 4 error(s), 0 warning(s)

    this looks like it is probably a simple answer. Any help is much appreciated.

    Where can I learn how to do this .dll stuff properly? the website didnt really explain anything.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You probably need to include <stdlib.h> for malloc() and free(). And prototype oldmain(). And oldmain() takes argc and argv, why aren't you passing it anything?

    You should use <> for "standard" header files like this.
    Code:
    #include "windows.h"
    ->
    Code:
    #include <windows.h>
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    prototype? how do i do that?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you have a function like this:

    Code:
    int add(int x, int y)
    {
    	return x + y;
    }
    The prototype could be either of these:

    Code:
    int add(int, int);
    Code:
    int add(int x, int y);
    The prototype needs to appear before a function is used.

    Incidentally, don't try to prototype standard functions (specifically don't try to write a prototype for main()!). Header files provide all the prototypes you need for standard functions that aren't built into the compiler (like main()).

  7. #7
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72
    http://www.codeproject.com/dll/XDllPt1.asp?print=true

    Took me 15 minutes to understand it. It's much easier than what I learned before with COM, of course, if you have VC++ 07 and up.

  8. #8
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    Please.cpp
    c:\...\please\please.cpp(4) : fatal error C1083: Cannot open precompiled header file: 'Debug/Please.pch': No such file or directory
    Error executing cl.exe.

    does anyone know what causes this?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The compiler has a setting for which file "generates" the precompiled header.

    If you use visual studion and go to
    project->properties->C/C++->Precompiled Headers

    you will see that is says
    "Create/Use Precompiled Headers"
    "Create/Use precompiled header through"

    Now, you need to compile the file that is set to "Create precompiled headers" - this is "stdafx.cpp" in a default project.

    A complete rebuild of your project should do the trick.

    --
    Mats

  10. #10
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    Ok i cant compile StdAfx.cpp I get this error

    Code:
    --------------------Configuration: StdAfx - Win32 Debug--------------------
    Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/StdAfx.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    
    StdAfx.exe - 2 error(s), 0 warning(s)
    all that is in StdAfx.cpp is
    Code:
    #include "stdafx.h"
    Thats is if i give it its own workspace.. if it put it in the project with please.cpp it cant compile either.

    the precompiled headers screen looks like for my please.cpp
    Attachment 7441
    ^pretend giveup = please.. i went through all the steps again

    ... my brain hurts
    Last edited by chico1st; 05-16-2008 at 09:28 AM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The "easy" solution is to just turn off precompiled headers. This is NOT a good idea on a slow machine/hard-disk if you include things like <windows.h>, but to get you going, that should be fine.

    By the way, I meant that you need to compile stdafx.cpp INSIDE YOUR PROJECT. If you try to build an executable from that on it's, as you've seen, it won't find a "main" - naturally, since that's elsewhere (or in your case, probably "nowhere", since you're trying to build a DLL).

    --
    Mats

  12. #12
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    ok nice it compiled, i will test it to see if it works tomorrow.
    thanks for all your help but one question

    shouldnt this give me a "Please.h" file.. i didnt get one.. dont i need it?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure what steps you went through to create please.cpp, but it's not NECESSARY to have a .h file for every .cpp file - it's common to have one for MOST .cpp files, but you don't HAVE to have one.

    --
    Mats

  14. #14
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    when I run the .exe version of this code I print messages to screen and I can see them but when i call the .dll version of this code I dont see those messages.

    Is this normal?
    Is there anyway to make these messages display?

    Im curious because the .dll doesnt seem to work but the .exe does, so im trying to figure out where it is going wrong. Any ideas?

    Once again thank you very much.

    *EDIT*
    Ok it works now, i just redid the steps, but still can i make it show the print to screen stuff?
    Last edited by chico1st; 07-26-2007 at 02:10 PM.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see any reason why a DLL wouldn't be able to use for example printf - what exactly are you doing, and what happens if you set a breakpoint around the printf in your DLL and single-step it?

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 01:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 12:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 07:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 08:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 01:58 PM

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