Frage

How do i call this method from java :

 WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

Is it possible to call WinMain from java ? I want the value of the arguments in that function be same as when directly running a c++ program. I want to link a .dll whose entry point be this function.

War es hilfreich?

Lösung

It is a bad idea, for multiple reasons.

WinMain is special

Like main or DllMain, those functions have more to say than it appears.

Contrarily to the naive approach, WinMain is not the "first" function on the stack. It is the first function writeable by the code developer. But if you go in debug, you'll see that there is a lot going on before and after WinMain.

Among them, construction/destruction of C++ global objects, functions registered with the atexit C API, etc., but you can be sure there's a lot happening there that is specific to the Windows platform.

Then, if it is a WinMain, chances are you have a message loop somewhere inside. It could well interfere with your own (is your Java application a GUI app?)

HINSTANCE is not optional

What parameter values will you give WinMain?

The first HINSTANCE parameter is quite important and could be used by the code of the executable you're trying to launch. You can't just feed some random value and expect it to work. You could retrieve the HINSTANCE of your Java process, but I suspect you wouldn't like the result.

Hidden variables are not optional

Let's say you succeed in calling the WinMain of a program. This program will expect some things to be there (see the WinMain is special section). Among them, the result of the GetCommandLine() API function, that could be used in your C++ program.

WinMain is for processes, not DLLs

Is it possible to call WinMain from java ? I want the value of the arguments in that function be same as when directly running a c++ program. I want to link a .dll whose entry point be this function

Are you trying to "launch" a DLL, whose entrypoint is WinMain? I guess there's something wrong somewhere. WinMain or main are the standard entrypoints on Windows for processes, not DLLs. A DLL entry point is usually DllMain which have a different prototype.

Conclusion

I don't know why you need to launch your executable in the same process than your Java launcher, but I believe you're doing something wrong.

Like mikera wrote in his answer, you'd better use the Java API to launch a process.

Andere Tipps

It should be possible through JNA:

However it still seems like a design smell to me..... why would you want to call WinMain (the standard Windows application entry point) from a Java program? Why not just Runtime.exec() it instead?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top