开发者

calling C++ "int WINAPI _tWinMain" from C#

开发者 https://www.devze.com 2023-02-10 15:09 出处:网络
I can access a method from a C++ D开发者_StackOverflow社区ll using C# using this method in the C++:

I can access a method from a C++ D开发者_StackOverflow社区ll using C# using this method in the C++:

extern "C"
{
  __declspec(dllexport) void DisplayHelloFromDLL()
  {
    printf ("Hello from DLL !\n");
  }
}

this works great...but the solution I am working with uses this as the entry point:

extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
                                HINSTANCE /*hPrevInstance*/,
                                LPTSTR lpCmdLine,
                                int /*nShowCmd*/)

Is there a way I can access this like I have done with the __declspec method?

Cheers


That is not a DLL entry point, that is a primary application entry point. You will need to create it as a new process via CreateProcess.


_tWinMain is actually a #define to either WinMain or wWinMain. You also need to make sure it's actually exported.

That being said, why would a DLL have a WinMain function at all? You should just export a normal function like DisplayHelloFromDLL.

[edit]

The project you are trying to reference -- the one with _tWinMain -- is an EXE (as @DeadMG says). You should not try to import its functions from C# like you do with DLLs; instead you should launch it with Process.Start.


The answer was to call a function created in the C++ using:

extern "C"
{
  __declspec(dllexport) void StartAgent()
  {
    printf ("Starting Agent... \n");

    StartServer(true);
    RunMainLoop();
  }
}

This is then called in the C# using:

[DllImport("myDll.dll")]
public static extern string StartAgent();

StartAgent();

Calling this from the C# and into the C++ gets the application running.

0

精彩评论

暂无评论...
验证码 换一张
取 消