0

when I call this CallExport function, GetProcAdress(module, "Main") returns 0 and I don't know why. The LoadLibraryEx just before works and returns a valid IntPtr.

public class ProcessInteraction {

   public static bool CallExport(Process process, string dll, string function)
     {
          IntPtr module = LoadLibraryEx(dll, IntPtr.Zero, 1);
          if (module == IntPtr.Zero) return false;

          IntPtr functionAddress = GetProcAddress(module, function);
          if (functionAddress == IntPtr.Zero) return false;
          functionAddress = GetModule(process, dll).BaseAddress + (int)functionAddress - (int)module;

          IntPtr thread = CreateRemoteThread(process.Handle, IntPtr.Zero, IntPtr.Zero, functionAddress, IntPtr.Zero, 0, IntPtr.Zero);
          if (thread == IntPtr.Zero) return false;

          WaitForSingleObject(thread, 0xFFFFFFFF);
          CloseHandle(thread);

          return true;
      }

 }

In my Dll code I have :

internal class EntryPoint {
     [DllExport("Main")]
     internal static void Main() {
           // some code ...
     }
 }

It seems like GetProcAdress(module, "Main") can't find the "Main" function inside my dll, even though there is one.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
guillaume guerin
  • 357
  • 2
  • 6
  • 17
  • 1
    "even though there is one" -- Are you sure? There is no `DllExport` functionality in standard .NET, and if some custom library and tool does claim to provide it, then it would be worth checking if it actually works. If you use any of the many tools to view the exports of a DLL, check with some well-known DLL (e.g. Windows's own `user32.dll`) that the tool works, and then use the tool on your own DLL. What do you see? Does `Main` actually get exported? If so, then those details would be worth including in your question. If not, then the question you should be asking is how to make *that* work. –  Oct 31 '15 at 10:08
  • [This](http://stackoverflow.com/q/11426310/5128464) might be interesting for you – vlp Nov 02 '15 at 20:35

0 Answers0