C++ with DLL problem!

This is a discussion on C++ with DLL problem! within the C++ Programming forums, part of the General Programming Boards category; Ok yesterday I tried getting Vb to work with my C++ dll, but that just didn't work. So now I ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    19

    C++ with DLL problem!

    Ok yesterday I tried getting Vb to work with my C++ dll, but that just didn't work. So now I am trying to get my C++ program to cal on my dll. However hen I compile my C++ test program with my DLL library I get a link error saying

    DllTest1 error LNK2001: unresolved external symbol "void __cdecl Test(void)" (?Test@@$$FYAXXZ)


    My Dll code for the Test() function is

    extern "C" void __cdecl Test(){
    MessageBox(NULL,"Dll Works!","YAY!",MB_OK);
    }

    What am I doing wrong? Thanks!!!!!
    May GOD Bless, Strengthen, Guide, and Protect you Always!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>extern "C" void __cdecl Test(){
    I'm guessing, but should it be:
    >>extern "C" void __cdecl Test(void){
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    Nope. I am still getting the same error. Any other ideas? Thanks!
    May GOD Bless, Strengthen, Guide, and Protect you Always!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    Hi!

    Try:

    int _stdcall Test(){
    MessageBox(NULL,"Dll Works!","YAY!",MB_OK);
    return 0;
    }

    Also, create a file with the name of your .cpp file. For example:
    MyDLL.def! The file should have .def extension. Add it to your project (the .def file) from your compiler. In the .def file write:

    LIBRARY MyDLL

    EXPORTS
    Test @1

    That should work! At least it works with me!
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I saw your other post. You're going around in circles, you had the C part right yesterday. Have you forgotten the __declspect bit?

    I've pasted some code of mine from an import/export header file. This file is used both in building the DLL & importing it into an app. See the #def LEXI_BUILD_API, which causes it to switch between dllexport and dllimport.

    You can use either __cdelc (the C calling convention) or __stdcall. Which one depends on who you want to be able use your DLL. If you use __cdecl, VB apps won't be able to use it.

    Why don't you find a site which has a simple example of building & linking a DLL. Get that working & use it as a template to do what you want.



    Code:
    ...
    #ifdef LEXI_BUILD_API
    #define LEXI_DLLIMEX dllexport
    #else
    #define LEXI_DLLIMEX dllimport
    #endif
    
    //---------------------------------------------------------------------------
    // TARA LEXI EXPORT/IMPORT FUNCTIONS
    //---------------------------------------------------------------------------
    extern "C"
    {
    ...
    ...
    
      // Debugging
      __declspec(LEXI_DLLIMEX) int __cdecl LexiVersionMajor();
      __declspec(LEXI_DLLIMEX) int __cdecl LexiVersionMinor();
      __declspec(LEXI_DLLIMEX) int __cdecl LexiErcToStr(int ierc, char* strBuf, int maxLen);
      __declspec(LEXI_DLLIMEX) void __cdecl LexiSetShowErc(const char* appTitle);
      __declspec(LEXI_DLLIMEX) bool __cdecl LexiDebug(const char* strKey, bool debugOn);
    }

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    The reason I keep posting Davros, is because I can't find any sites that actually work. What I mean by that is their tutorials, they aren't working. I keep getting the same errors. I am using VC++ .NET

    I just tried you idea. Here is my entire code for my dll.

    #include "stdafx.h"
    #include <mmsystem.h>
    #include <stdlib.h>
    #include <stdio.h>

    #ifdef DREAMLIB_API
    #define DREAMLIB dllexport
    #else
    #define DREAMLIB dllimport
    #endif
    extern "C" __declspec(DREAMLIB) int __cdecl Test();


    BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
    {

    return TRUE;

    }

    extern "C" int __cdecl Test(){
    MessageBox(NULL,"Dll Works!","YAY!",MB_OK);
    return 0;
    }

    Along with my DLL code I added a .def to my project which says

    LIBRARY MyDLL

    EXPORTS
    Test @1

    Here is the C++ sample app I wrote to test the dll. In this project, I included the DreamLib.Lib that was exported with my dll.

    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>

    int Test();

    void main()
    {
    Test();

    }


    Here is the only error I get when compiling his Test app.

    DllTest1 error LNK2001: unresolved external symbol "int __cdecl Test(void)" (?Test@@$$FYAHXZ)


    I hope this helps you help me =) Thanks again!
    May GOD Bless, Strengthen, Guide, and Protect you Always!

  7. #7
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    How are you linking to your DLL. You either have link against an object file created when you build your DLL, OR load the library explicitly using LoadLibrary. Personally, I prefer the latter approach, but most people seem to prefer the former.

    Where are you including the import header file?

    int Test();

    Edit : Hangon - I see it now. You're linking against the obj file.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    So Davros, how do I not link against the object file? Thanks!
    May GOD Bless, Strengthen, Guide, and Protect you Always!

  9. #9
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    You call LoadLibrary at runtime & assign function pointers to hook addresses in the DLL. You get the addresses by calling GetProcAddress. Linking against an object library (to access a dll) is called implicit linking, while doing this at runtime is called explicit linking. For once, the MSDN documentation covers this subject really well.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    19
    Davros, can I see an example of that? Do you know a site with a tutorial like that.

    I don't understand why all that is nessicary though. I see so many other people compiling dlls with just one function not having of do a lot of extra work and they say it is successful for them. Why not with me? You said it is because I'm linking against the object Well is there another I can link without having to do all the extra work?
    Last edited by FwyWice; 12-03-2002 at 02:26 PM.
    May GOD Bless, Strengthen, Guide, and Protect you Always!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL Function / Load Library Problem
    By cboard_member in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2005, 09:11 AM
  2. Replies: 1
    Last Post: 09-18-2005, 09:06 PM
  3. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 04:29 AM
  4. std::string vs char* DLL problem
    By aker_y3k in forum C++ Programming
    Replies: 13
    Last Post: 10-02-2002, 09:05 AM
  5. VCL and DLL class problem
    By borland_man in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2002, 10:07 AM

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