0

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.

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

2 Answers2

4

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.

Community
  • 1
  • 1
paercebal
  • 81,378
  • 38
  • 130
  • 159
  • _"A DLL entry point is usually DllMain"_ . I didn't know that ! This is the reason i was using `WinMain` – Suhail Gupta May 29 '12 at 12:51
  • "It is the first function writeable by the code developer" in the general case this is true, but you can override the CRT entry points on most compilers, allowing you to skip `WinMain` entirely – Necrolis May 29 '12 at 13:28
  • @Suhail Gupta : The DllMain function is *NOT* the same thing as the WinMain function. It is not called the same way, it has different constraints, etc.. So, do NOT use the DllMain as a WinMain. Instead, if you have control of your library, create some exported function like "MyOwnEntryPoint" with the prototype you want, and then call it like a normal function. – paercebal May 29 '12 at 13:57
  • @Necrolis : Of course, you're right (note that it is explained in the WinMain link I offered in the answer). But I did not press that particular issue because the original question had other problems to solve. – paercebal May 29 '12 at 13:59
  • [can you help answer this question](http://stackoverflow.com/questions/10731172/cannot-see-the-message-when-user-presses-the-key) this question was based upon the question inside the link – Suhail Gupta May 30 '12 at 04:09
2

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?

mikera
  • 105,238
  • 25
  • 256
  • 415
  • _Runtime.exec() :_ I don't want to start a separate process but want a communication between `dll` and `java` program – Suhail Gupta May 29 '12 at 12:03
  • 1
    Unless I'm wrong, there's a lot of code going just before, and after WinMain, so calling WinMain directly will *at the very least* let some global variables uninitialized and/or non-destroyed. I'm not even sure it will work *at all*. – paercebal May 29 '12 at 12:04
  • @SuhailGupta: if you don't want to start a separate process, why would you call WinMain to communicate with a running Windows program? I'm confused as to exactly what your goal is. Please clarify. – Hovercraft Full Of Eels May 29 '12 at 12:10
  • @Hovercraft Full Of Eels I wan't to accomplish a task that cannot be completed till i call the native code written in C++. Till now that native code is a standalone application.[As the C++ application starts `WinMain` is called and does it work. One of the first statement in my `WinMain` method is `SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0)` and there is also a message loop in there](http://i46.tinypic.com/11caxs0.jpg).Now i want to call this code from java. _(linking it as dll)_ At some point i will make this code return me some values. – Suhail Gupta May 29 '12 at 12:19
  • If you want to create your own Window's keyboard hook, then that's not the way to do it. If you want to communicate with an existing C++ application, that's not the way to do it. – Hovercraft Full Of Eels May 29 '12 at 12:28
  • @Hovercraft Full Of Eels I know calling `WinMain` seems to be a poor decision but is there any way i can do it ? – Suhail Gupta May 29 '12 at 12:36
  • @Suhail: Do ***what*** exactly? Please tell more of the important details of just what you're trying to do. – Hovercraft Full Of Eels May 29 '12 at 12:37
  • @Hovercraft Full Of Eels My aim is executing the code i showed you in the `WinMain` method. By this way i am able to register the hook and with the help of message loop, windows is able to call the the hook-procedure. The point is the reference of HINSTANCE `setWindowsHookEx` receives. I can't do the same from any other method. Also how am i going to process the message loop outside `WinMain`. For example see [this question earlier posted by me](http://stackoverflow.com/questions/10731172/cannot-see-the-message-when-user-presses-the-key/10733415#10733415) It will tell you what i want to do – Suhail Gupta May 29 '12 at 12:42