Calling Java 8 app from Delphi

Hi All:



I have been using the procedure below to call a Java program for years. For 
some reason it no longer works after the machine is upgraded Java 8u25.  If 
fails when it tries to create the process.  Any idea of why it would not 
work with Java 8?  Note if I paste the same command line I use in the Delapi 
XE6 application into a CMD prompt or downgrade to Java 7 it works just fine. 
Is there a modification to the WinExecAndWait procedure  I need to make? 
Thanks  Bob



function WinExecAndWait32(CommandLine: string; ShowWindow: Word): DWORD;



  procedure WaitFor(ProcessHandle: THandle);

  var

    msg: TMsg;

    ret: DWORD;

  begin

    repeat

      ret := MsgWaitForMultipleObjects(

               1,             { 1 handle to wait on }

               ProcessHandle, { the handle }

               False,         { wake on any event }

               INFINITE,      { wait without timeout }

               QS_PAINT or    { wake on paint messages }

               QS_SENDMESSAGE { or messages from other threads }

               );

      if ret = WAIT_FAILED then Exit; { can do little here }

      if ret = (WAIT_OBJECT_0 + 1) then

      begin

        { Woke on a message, process paint messages only. Calling

          PeekMessage gets messages send from other threads processed. }

        while PeekMessage(msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE) do

          DispatchMessage(msg)

      end

    until ret = WAIT_OBJECT_0

  end;



var

  zAppName: array[0..512] of Char;

  StartupInfo: TStartupInfo;

  ProcessInfo: TProcessInformation;

begin

  StrPCopy(zAppName, CommandLine);

  FillChar(StartupInfo, Sizeof(StartupInfo), #0);

  StartupInfo.cb := Sizeof(StartupInfo);

  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;

  StartupInfo.wShowWindow := ShowWindow;

  if not CreateProcess(nil,

    {pchar (CommandLine)} @zAppName, { pointer to command line string }

    nil,                  { pointer to process security attributes }

    nil,                  { pointer to thread security attributes }

    False,                { handle inheritance flag }

    CREATE_NEW_CONSOLE or { creation flags }

    NORMAL_PRIORITY_CLASS,

    nil,                  { pointer to new environment block }

    nil,                  { pointer to current directory name }

    StartupInfo,          { pointer to STARTUPINFO }

    ProcessInfo)          { pointer to PROCESS_INF }

  then

    Result := DWORD(-1)   { failed, GetLastError has error code }

  else

  begin

     WaitFor(ProcessInfo.hProcess);

     GetExitCodeProcess(ProcessInfo.hProcess, Result);

     CloseHandle(ProcessInfo.hProcess);

     CloseHandle(ProcessInfo.hThread)

  end

end;
0
Bob
10/24/2014 8:12:42 PM
embarcadero.delphi.win32 2183 articles. 0 followers. Follow

6 Replies
783 Views

Similar Articles

[PageSpeed] 58

Bob wrote:

> I have been using the procedure below to call a Java program
> for years. For some reason it no longer works after the machine
> is upgraded Java 8u25.  If fails when it tries to create the process.

Fails how exactly?  Does CreateProcess() itself fail with an error code? 
 If so, what error code exactly?  Or does the Java app start and then fail? 
 You need to be more specific.

> Is there a modification to the WinExecAndWait procedure I need
> to make?

There is nothing wrong with the code you have shown (though I would suggest 
you get rid of the zAppName array to avoid any potential buffer overflow 
for long command lines.  You can use UniqueString(CommandLine) and PChar(CommandLine) 
instead), so it has to be a problem with either the Java app itself, or the 
CommandLine you are using.  What does the CommandLine actually look like?

--
Remy Lebeau (TeamB)
0
Remy
10/24/2014 8:33:17 PM
Thanks, CreateProcess() fails and  I found the problem, I have to specific 
the full path to the Java.exe file in my command line.  Very strange since I 
didn’t have to do that with the previous version of Java.

How would the code look with UniqueString, I am not familiar with that but 
like your idea.  Thanks Bob



"Remy Lebeau (TeamB)"  wrote in message 
news:699225@forums.embarcadero.com...

Bob wrote:

> I have been using the procedure below to call a Java program
> for years. For some reason it no longer works after the machine
> is upgraded Java 8u25.  If fails when it tries to create the process.

Fails how exactly?  Does CreateProcess() itself fail with an error code?
If so, what error code exactly?  Or does the Java app start and then fail?
You need to be more specific.

> Is there a modification to the WinExecAndWait procedure I need
> to make?

There is nothing wrong with the code you have shown (though I would suggest
you get rid of the zAppName array to avoid any potential buffer overflow
for long command lines.  You can use UniqueString(CommandLine) and 
PChar(CommandLine)
instead), so it has to be a problem with either the Java app itself, or the
CommandLine you are using.  What does the CommandLine actually look like?

--
Remy Lebeau (TeamB)
0
Bob
10/24/2014 8:40:33 PM
Bob wrote:

> Thanks, CreateProcess() fails and  I found the problem, I have to
> specific the full path to the Java.exe file in my command line.  Very
> strange since I didn’t have to do that with the previous version of
> Java.

Most likely the previous version had an entry in the system PATH whereas 
the new version does not.

> How would the code look with UniqueString, I am not familiar with
> that but like your idea.

Like this:

{code}
function WinExecAndWait32(CommandLine: string; ShowWindow: Word): DWORD;
....
var
  ...
begin
  UniqueString(CommandLine); // make sure the variable is the only reference 
to the character data
  ...
  if not CreateProcess(nil,
    PChar(CommandLine), { pointer to command line string }
    ...
end;
{code}

--
Remy Lebeau (TeamB)
0
Remy
10/24/2014 9:12:34 PM
>UniqueString(CommandLine); <
That actually works, I get an access violation at runtime if I don’t use it. 
Can you explain what it is doing and why it avoids the violation?  thanks 
bob
0
Bob
10/24/2014 10:07:01 PM
Bob wrote:

> That actually works, I get an access violation at runtime if I
> don’t use it. Can you explain what it is doing and why it avoids
> the violation?

String data is reference counted.  UniqueString() ensures that the specified 
String is referring to a writable block of memory that has a reference count 
of 1.  If the String data is being referred to by mutiple Strings (reference 
count > 1) or the data is a string literal (reference count is -1), the character 
data is copied into a new writable memory block with a reference count of 1.

In Delphi 2007 and earlier, CreateProcess() maps to CreateProcessA(), whose 
second parameter is read-only, so it is safe to pass literals to it, and 
UniqueString() is not needed.

In Delphi 2009 and later, CreateProcess() maps to CreateProcessW(), whose 
second parameter must point at writable memory, so UniqueString() (or a separate 
allocated buffer) is needed if the String refers to a literal.  This is documented 
on MSDN:

CreateProcess function
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425.aspx

{quote}
lpCommandLine [in, out, optional]
....
The Unicode version of this function, CreateProcessW, can modify the contents 
of this string. Therefore, this parameter cannot be a pointer to read-only 
memory (such as a const variable or a literal string). If this parameter 
is a constant string, the function may cause an access violation.
{quote}

--
Remy Lebeau (TeamB)
0
Remy
10/24/2014 10:49:01 PM
thanks much!!

"Remy Lebeau (TeamB)"  wrote in message 
news:699249@forums.embarcadero.com...

Bob wrote:

> That actually works, I get an access violation at runtime if I
> don’t use it. Can you explain what it is doing and why it avoids
> the violation?

String data is reference counted.  UniqueString() ensures that the specified
String is referring to a writable block of memory that has a reference count
of 1.  If the String data is being referred to by mutiple Strings (reference
count > 1) or the data is a string literal (reference count is -1), the 
character
data is copied into a new writable memory block with a reference count of 1.

In Delphi 2007 and earlier, CreateProcess() maps to CreateProcessA(), whose
second parameter is read-only, so it is safe to pass literals to it, and
UniqueString() is not needed.

In Delphi 2009 and later, CreateProcess() maps to CreateProcessW(), whose
second parameter must point at writable memory, so UniqueString() (or a 
separate
allocated buffer) is needed if the String refers to a literal.  This is 
documented
on MSDN:

CreateProcess function
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425.aspx

{quote}
lpCommandLine [in, out, optional]
....
The Unicode version of this function, CreateProcessW, can modify the 
contents
of this string. Therefore, this parameter cannot be a pointer to read-only
memory (such as a const variable or a literal string). If this parameter
is a constant string, the function may cause an access violation.
{quote}

--
Remy Lebeau (TeamB)
0
Bob
10/24/2014 11:28:51 PM
Reply:

Similar Artilces:

Java app calling a Delphi DLL
Hello I have a Java application that must call functions inside a large D7 dll. For that, I used JNI calls. That part is OK and the delphi dll is receiving the function parameters smoothly. The Delphi dll has a couple Forms in it. And these Forms are unresponsive. Timers don't work. Buttons will not invoke OnClick handlers. Most important, the assync sockets in the forms, which the dll uses to deliver its work, are not functioning either. It seems that the dll is not receiving Windows messages. I tried to simulate the application loop, but failed. The same DLL works fine i...

Calling Delphi 6 DLL from Delphi 2010
We have a Delphi 6 dll that has a PChar passed to it. The dll function then does a search and returns a PCHar. This works good using Delphi 6 to call and recieve the dll result. We have upgraded our calling program to Delphi 2010 and most of the time, the value is passed back just fine. However, we do have times when we get an access violation, when calling the dll. The dll does process the request. Below is the code. Has anyone had a similar issue using an older version Delphi Dll with Delphi 2010? I am sure that Unicode has something to do with it, but not sure how to fix th...

Migrate from Delphi 2007 for Win32 to Delphi XE
we use Delphi 2007 for Win32 to support legacy (32Bit) OWL-based pascal applications (yes i know it was a mistake not to switch to VCL 15 years ago). could our applications still be opened and compiled with Delphi XE? The existing projects are all plain Pascal-Code, coming back from the times of Turbo Pascal for Windows and later on Borland Pascal. Are there any improvements we could profit from (i.e IDE, Debugger)? Thanks Andrej > {quote:title=Andrej Dimic wrote:}{quote} > could our applications still be opened and compiled with Delphi XE? I'm not sure, but I guess ...

delphi Win32 using delphi .NET dll
Hi, I'm trying to use a delphi.NET dll in delphi.WIN32. I am currently using CodeGear Delphi 2007 with version2(base version) of .NET I can get the dll to import into the WIN32 application the only problem is when i include things such as: "using Classes,DateUtils, SysUtils" in the .NET dll the win32 application will instantly hang when any of the dll functions are called. Any help would be great thanks. Also I have tried this example and it also crashes for me? http://cc.embarcadero.com/Item/22688 -Braden I also found this.. "The problem is that, wehn you instal...

Delphi.NET loading Delphi.Win32 Driver
Hi, What I'm trying to do is marshal an array of cardinal (or integer) back into managed memory from a win32 dll. I know how to pass managed memory into a win32 dll {code} var aa : array of Integer; Buffer : IntPtr; begin SetLength(aa,2); aa[0] := 1; aa[1] := 80; if not Supports(ExtractFilePath(Application.ExeName)+'Win32_Library\SDK_Driver.Win32.io', TypeOf(IMyFunctions), MyFunctions) then Exit; //loads the driver into memory. MyFunctions contains the method names found in the SDK_Driver. Buffer := Marshal.AllocHGlobal(2 * {Marshal.SystemDefaultC...

Converting Delphi for Win32 to Delphi .Net(Prism)
Hi, I am currently migrating a project from Delphi for Win32 to Delphi.net. Part of my code currently goes into a directory and pulls out a random file from this directory and loads the contents of the file for me. This code doesn't seem to work in Delphi.Net. It uses PString and a number of functions in SysUtils that don't seem to be present in Delphi.net's SysUtils file. If anyone can help me please, it would be greatly appreciated! Many thanks, Jonathan Mackey Jonathan Mackey a écrit : > I am currently migrating a project from Delphi for Win32 to &...

Delphi XE7 call Java Class, JAR, such as the example of Zxing call
Delphi XE7 call Java Class, JAR, such as the example of Zxing call http://zarko-gajic.iz.hr/firemonkey-mobile-android-ios-qr-code-generation-using-delphi-xe-5-delphizxingqrcode/ Generate interface files with Java2pas: http://www.fmxexpress.com/utility-to-generate-java-android-class-and-jar-file-interfaces-for-delphi-xe5-firemonkey/ Am 14.09.2014 10:08, schrieb ww6 wuxu: > Delphi XE7 call Java Class, JAR, such as the example of Zxing call > Hello, I assume this is some Android related question? If that's the case and the other posted reply isn't yet what you want...

calling a c++ dll from a delphi vcl app crashes the app upon exit [Edit]
I am using RAD Studio 2010 and I downloaded the latest updates. Since I can't see how I can attach a file to the message, I included in this message the source code for each file: To reproduce this problem follow these steps: Create a project group with two projects, one c++ dll project and one a Delphi VCL forms project. Save all the files in the same folder. 1. Use the wizard to create the c++ dll project, select c++ & Use VCL checkboxes. Make sure Project Options->C++ Linker->Dynamic RTL is set to true. I also added memmgr.lib and sharemem.obj to the project but I bel...

Win32 program: Delphi 7 vs Delphi XE5
How is a D7 Win32 program compared to a Delphi XE5 one in terms of stability and performance? Is Delphi XE5 good enough for a big ERP project with several DLL's and hundreds of units and forms? Thanks in advance Am 26.12.2013 15:02, schrieb lior ilan: > How is a D7 Win32 program compared to a Delphi XE5 one in terms of stability > and performance? > Is Delphi XE5 good enough for a big ERP project with several DLL's and > hundreds of units and forms? > Thanks in advance > Hello, XE5 has increased functionality. Stability seems to be ok for most ...

Delphi and Delphi for .Net
It seems that Delphi for .Net is slower than Delphi Win32 native applicaiton. I would like to know is it true all .Net application is slower than Win32 native applicaiton or it is Delphi for .Net only. Your information is great appreciated, Inung On 2011-06-21 18:20:17 +0100, Inung Huang said: > It seems that Delphi for .Net is slower than Delphi Win32 native applicaiton. > I would like to know is it true all .Net application is slower than > Win32 native applicaiton or it is Delphi for .Net only. If you are only running the code in the application once then, yes, yo...

Win32 Delphi language features introduced since Delphi 7
Hi, Am I right in thinking that the language features introduced since Delphi 7 fall into the categories: a) language features dictated by .Net compatibility. e.g. Namespaces, Inlining, records with methods, operator overloading, pure interfaces, generics, extended RTTI and reflection; b) Unicode strings and supporting procedures? c) 64-bit support What other language features, if any, have been introduced since D7? Had most of the post-D7 languages features, except for generics, Unicode strings, and 64-bit support, been introduced in or before Delphi 2005? How bug-free were ...

problem delphi 2010 apps with a delphi 5 pro Pchar
Hallo I build on till now alle tools and programas with delphi 5 pro now i have delphi 2010 want don't want rewite alle my DLL Tools Like The tool i made for making PDF In case of This DLL i can't becose the vcl i use in delphi 5 will not work in delphi 2010 *This call in apps* {code} Function GenratePDF(Settings:Pchar):Boolean; stdcall external 'GUPPDF2.dll'; {code} *In delphi 5 DLL* {code} Function GenratePDF(Settings:Pchar):Boolean; stdcall; {code} if call the function i get a "Access violation" i think that the settings parameter empty is...

Calling Win32 DLL from Delphi Prism
I have installed Delphi Prism and try to call a Win32 DLL (I posted a message previously, but since it is difficult to help a client remotely, I decided to test this myself). I would like to call the following two functions from MyLib.DLL. What would be the minimal requirement to call those procedures: function WL_LoadModelFile(Filename: PChar): Integer; stdcall; function WS_UnloadModel: Boolean; stdCall; Normand Hi, > I have installed Delphi Prism and try to call a Win32 DLL (I posted a > message previously, but since it is difficult to help a client remotely, I >...

calling java program from Delphi 2010
hello all, Is there a way I can call a java program installed on my machine and have it load into a window controlled by my application? Basically trying to prevent the user from being able to alt-tab between the two applications, Thanks Bob On 2010-06-09 3:22 PM, Bob McKinnon wrote: > hello all, > > Is there a way I can call a java program installed on my machine and have it load into a window controlled by my application? Basically trying to prevent the user from being able to alt-tab between the two applications, Thanks Bob If you can somehow find the Window Handle...

Web resources about - Calling Java 8 app from Delphi - embarcadero.delphi.win32

Calling Dr. Death - Wikipedia, the free encyclopedia
Calling Dr. Death (1943) is the first of the Universal Pictures Inner Sanctum mystery films . The "Inner Sanctum" franchise originated with a ...

List of country calling codes - Wikipedia, the free encyclopedia
This is a list of country calling codes defined by ITU-T recommendations E.123 and E.164 , also called IDD ( International Direct Dialling ) ...

Calling Batman: Bane Takes Over Ecko Unlimited Facebook Page
Bane, the villain in upcoming Batman feature film The Dark Knight Rises , turned his criminal mind toward the Ecko Unlimited Facebook page , ...

Facebook Video Calling - Facebook
Bring your conversations to life on Facebook. With face-to-face video calling, now you can watch your friends smile, wink and LOL. To get started, ...

Facebook’s Talktime program in India pays new mobile users in calling credit
... Facebook has launched its Talktime program in India which gives every new user who signs up with a mobile device 50 Rupees (almost $1) in calling ...

Berlin Calling (@neuraum) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Calling all foodies: Here comes @TwitterFood
There are many thousands of food-related Tweets people send on Twitter each day – Tweets about meals, ingredients, favorites, recipes and dining ...

Rounds Video Calling + Chat for iPhone 4, iPhone 4S, iPhone 5, iPhone 5c, iPhone 5s, iPad 2 Wi-Fi, iPad ...
Get Rounds Video Calling + Chat on the App Store. See screenshots and ratings, and read customer reviews.


Video Calling Coming to WhatsApp Soon: Report
... images show WhatsApp working on a prototype video call feature. Facebook Inc. ( NASDAQ:FB )-owned WhatsApp could soon introduce video calling ...

Resources last updated: 1/2/2016 1:14:41 AM