Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6586279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:46:16+00:00 2026-05-25T16:46:16+00:00

I know that under Linux you can get a handle to the calling program

  • 0

I know that under Linux you can get a handle to the calling program with dlopen( NULL, RTLD_* ). Is there a way to do so in Windows using LoadLibrary? I’ve tried using LoadLibrary(NULL) which doesn’t work, the handle variable is NULL, and I’ve tried by specifying the executable file as well and that doesn’t seem to be working either.

Running the following code just now got me 0x011b0000 in handle but the call to GetProcAddress is still returning NULL.

From commands.h
typedef std::tr1::shared_ptr<command> cmd_ptr;
typedef void (_cdecl *CMD)( creature &, string & );

From commands.cpp
HMODULE handle; // <--- Declared towards top of the file

void load_commands() // <--- Called from main
{
   handle = LoadLibrary(L"another-mud-project-src.exe");
   std::ifstream cmd_file(COMMAND_FILE);
   cmd_ptr cmd;

   if( cmd_file.is_open() )
   {
      std::string tmp, hold;
      while( true )
      {
         cmd_file >> tmp;
         if( tmp == "Name" )
            cmd_file >> hold;
         else if( tmp == "Code")
         {
            cmd_file >> tmp;
            cmd = std::make_shared<command>(hold,tmp);
         }
         else if( tmp == "Type" )
         {
            cmd_file >> tmp;
            cmd->set_cmdtype(atoi(tmp.c_str()));
            if( cmd->is_valid() )
               cmd_vec.push_back(cmd);
            else
               cmd.reset();
         }
      }
   }
}


void command::set_code( string & code )
{
   code_ = code;
   if( (func = (CMD)GetProcAddress( handle, code.c_str() )) == NULL )
   {
      std::cerr << "Could not find function name: " << code.c_str() << std::endl;
      code_.clear();
   }
}

Assuming I’m doing the right thing to allow the program to get a good handle on itself, is there anything special I need to do so GetProcAddress will find the functions as desired? I’ve even tried putting EXP_CMD, #define EXP_CMD __declspec(dllexport), in front of a function which doesn’t work either.

I’m using VS2010 under Windows 7 Pro x64. Compiling as a 32 bit console app.


Edit

Thanks for the pointer on using GetModuleHandle(NULL) Cat and Michael. Unfortunately GetProcAddress, which I assume I still need to use, is still returning NULL values. After sticking a call to GetLastError() in there I get 127 for the error code. I’ve tried putting extern "C" in front of the function, based on a bit more research, but I’m still getting a NULL return with error code 127.


Edit 2

After a bit more searching it seems that Visual Studio still mangles the names anyways and I might need a definition file to go along with the executable. Is there some way to get rid of that extra bit of mangling through code or in Visual Studio?


Edit 3

Alright. I appear to have gotten it worked out more or less. Taking a technique I saw in some other code I added

#define CMDF( name ) \
    extern "C" __declspec(dllexport) \
    void (name)( creature & ch, string & argument )

into commands.h and wrapped my desired functions in it, CMDF( func_name ), and it seems to be working as desired. Had to add the __declspec(dllexport) bit and that seems to have done the trick.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:46:17+00:00Added an answer on May 25, 2026 at 4:46 pm

    The handle to your own process is already open, you should not use LoadLibrary to open a new one. Use GetModuleHandle (specifically, GetModuleHandle(NULL)) or instance handle from WinMain instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that you can insert multiple rows at once, is there a way
How can I get the windows version I am currently running under Cygwin? I
How can i change values in the binary plist under linux os? I know
Is there a way to get a notification that a thread no longer runs
I know that I can do something like $int = (int)99; //(int) has a
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
Is there a manual for cross-compiling a C++ application from Linux to Windows? Just
I know there have been several questions in the past regarding things that compile
Sometimes, for whatever reason, I have to produce patch-files (under Linux) that are in
Under Linux, how can I tell what specific process owns / is using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.