Thread: GetProcAddress of string

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    GetProcAddress of string

    I created a small DLL and I want it to have an identifier of sorts, so I tried placing this in the DLL:
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
      __declspec(dllexport) const char* NAME = "SomeName";
    #ifdef __cplusplus
    }
    #endif
    And then used GetProcAddress to get the address of this variable:
    Code:
    //...after loading the DLL
    name = (char*)GetProcAddress(hDll,"NAME");
    GetProcAddress doesnt fail; however, name just contains garbage.

    Defining functions in the DLL works fine, ie:
    Code:
    extern "C" __declspec(dllexport) void SomeFunc() { /*...*/ }
    Any suggestions
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well. Maybe it has something to do with it being a string literal. Maybe try:
    Code:
    char NAME[] = {'H','e','l','l','o','\0'};
    Other than that I couldn't think of anything. Of course I have not even touched a dll
    Woop?

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    I don't think you can do this with variables, only with procedures, I may be wrong though, but I thought that's why it was GetProcAddress.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well...
    Code:
    __declspec(dllexport) char NAME[] = "Something" ; //works
    __declspec(dllexport) const char NAME[] = "Something"; //error C2201: 'NAME' : must have external linkage in order to be exported/imported
    __declspec(dllexport) const char* NAME = "Something"; //no compile errors, gives garbage at runtime
    Somehow I never knew there was a difference. Guess I'll pay more attention now...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    The easy option it to return the string from an exported function.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by JaWiB
    Code:
    __declspec(dllexport) char NAME[] = "Something" ; //works
    __declspec(dllexport) const char NAME[] = "Something"; //error C2201: 'NAME' : must have external linkage in order to be exported/imported
    __declspec(dllexport) const char* NAME = "Something"; //no compile errors, gives garbage at runtime
    I'm guessing that GetProcAddress is returning &NAME. This will work for an array as &array returns the same value as array (at least on Windows). You could test this out by dereferencing:
    Code:
    __declspec(dllexport) const char* NAME = "SomeName";
    
    //...after loading the DLL
    name = *( (char**) GetProcAddress(hDll,"NAME") );
    I agree with Fordy that returning the value from an exported function is a better option.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM