1. jalil Rehman
  2. PowerBuilder
  3. Friday, 24 May 2024 00:32 AM UTC

Hi Folks

The following function ShellExecute  works well for the 32 bit application but not for the 64 bit application .it always return with error =2 which is file not found.

I am using Pb build 2022 R3 3289

Is anything differnt I have to do for 64 bit application. ?

 

function long ShellExecute ( int hWnd, String saction,  String sFlnm, String sParam, String sDir, long iShowWin) library "Shell32.dll" alias for "ShellExecuteW"

Accepted Answer
John Fauss Accepted Answer Pending Moderation
  1. Friday, 24 May 2024 05:40 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi, Jalil -

I'm actually a little surprised the external function declaration you supplied works in 32-bit, but not surprised at all that it fails in 64-bit.

The first argument parameter (hWnd) is a Windows O/S Handle (ID) to a window, and handles are always of the same bitness as the application. In other words, in a 32-bit app a handle is a 32-bit integer, or a PB Long or ULong... not a 16-bit PB Integer. In a 64-bit app a handle is a 64-bit integer, or PB LongLong.

However, the "Longptr" data type introduced in PB 12.6 is the ideal choice for this parameter, as its size matches the bitness of the application. Therefore, change the data type of the first argument parameter to Longptr. Having the Longptr data type available and using it allows this one external function declaration to work in both 32-bit and 64-bit, without any bitness-specific coding!

You should also change the data type of the API function's return value to Longptr, as this API function returns a HINSTANCE value (Handle to an instance), which is also a type of Windows handle.

Change the external function declaration, but also change any code that calls this function to use the Longptr data type and the code should work when compiled for either 32-bit or 64-bit.

Best regards, John

Comment
  1. Benjamin Gaesslein
  2. Friday, 24 May 2024 09:02 AM UTC
I suspect PB internally uses longs for integers in some cases. This might be one of them.
  1. Helpful
  1. Miguel Leeuwe
  2. Friday, 24 May 2024 10:16 AM UTC
Long (no pun intended) time ago, I seem to remember having read somewhere, that all integer variables are converted to longs internally when running. So that would be an explanation.
  1. Helpful
  1. John Fauss
  2. Friday, 24 May 2024 14:14 PM UTC
Even though using a PB Long (or ULong) for a Windows handle works today, it is not technically correct, as the WinAPI functions expect a handle to be a 64-bit integer in a 64-bit app (try it in C/C++ to see how quickly it gets flagged as an error). Also, when a WinAPI structure contains a Windows handle, the in-memory layout of the structure elements will likely be incorrect if the structure definition specifies a data type of Long instead of Longptr for a handle, since 8-byte integers must be aligned on a starting memory address that is evenly divisible by 8 and 4-byte integers align on 4-byte boundaries. The in-memory placement of structure members that follow the member containing a handle may end up in the wrong starting offset.

If you're going to code interfaces to and utilize functions in external DLL's, not just Windows API functions, I strongly believe it is best for everyone to understand the rules, why those rules exist, and how to correctly code to those rules.

I feel that to do otherwise is a disservice to our compatriots in the Community that are asking for assistance.
  1. Helpful 1
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Friday, 24 May 2024 14:32 PM UTC
  2. PowerBuilder
  3. # 1

Here is the proper definition:

Function Long ShellExecute ( LongPtr hwnd, String lpOperation, String lpFile, String lpParameters, 
String lpDirectory, Long nShowCmd ) Library "Shell32.dll" Alias For "ShellExecuteW"

Any of the String arguments that you aren't using should be changed to LongPtr and pass zero when calling it. The hwnd argument is either a window Handle or zero.

I use Long for the return because the documentation states that it isn't a real HINSTANCE, they defined it that way for compatibility with 16-bit Windows.

Success is any value greater than 32.

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.