1. Balaji Pinjala
  2. PowerBuilder
  3. Tuesday, 27 August 2019 13:27 PM UTC

Hi All, 

We are using windows function WNetUseConnectionA and WNetCancelConnectionA in our power builder application to connect to a NAS drive which is not working and getting error message saying Mapping is failed due to multiple connections to the same NAS mount. 

We are accessing a file from a folder of the NAS drive and then we are writing a new file on the other folder of the same NAS drive. during both the actions both the functions are getting used to establish and cancel the connections. Its working fine when we are accessing the file but getting error when we try to write the file from the same screen. 

This is working fine in our 32 bit application which run on windows 2008 server. 

We are getting error when we deploy the same application in 64 Bit in windows 2016 server. 

Any immedieate help is much appreciated as this is a show stopper for one of our prod implementation. 

Please advise. Thank You. 

 

 

 

 

John Fauss Accepted Answer Pending Moderation
  1. Wednesday, 28 August 2019 04:00 AM UTC
  2. PowerBuilder
  3. # 1

Baliji - Here are suggestions as to how to troubleshoot 64-bit migration of WinAPI calls. Apologies for the length, but there are several items that need to be clearly explained in order to be helpful.

1. I'll repeat myself from earlier posts - Keep your app 32-bit. Your 32-bit app works and you can avoid what could be extremely frustrating hours/days trying to get these WinAPI calls working in 64-bit. There are clues & hints & examples on the web, but you may not easily find all of pieces to the puzzle. The Microsoft documentation of WinAPI calls largely ignores 64-bit ramifications. What follows here may not be all the pieces of the puzzle you will need, either.

2. Create a small, simple, single-window PB test application and work with it to figure out the details. Use the ProcessBitness member of the Environment object to determine bitness when the test app is executing. Copy in the 32-bit code from your "real" app and verify this works when running the test app from the PB IDE, which is 32-bit. Once you get the WinAPI calls working in your test app in 32-bit, leave this code alone. Use a separate button, etc. for 64-bit testing and verify the process bitness. You'll have to Build/Deploy your test app exe & pbd in 64-bit in order to test it, probably many, many times. Also, if you're not already using the WinAPI GetLastError function, you'll probably want to, at least in the test app, if WinAPI documentation indicates error conditions are accessible via GetLastError.

3. Handles. Face it, the WinAPI is handle-happy. Just about everything is identified with a handle. hWnd is a handle to a window, hDC is a handle to a device context, hBitmap, hFont, hProcess, etc., etc., etc. In a 32-bit process, a handle is a 32-bit, unsigned integer value (an UnsignedLong, or ULong in PB). In a 64-bit process, handles are 64-bits. PB has a cool, relatively new datatype: LongPtr (Long Pointer). When the PB app is executing in 32-bit, a LongPtr variable occupies 4 bytes. When the PB app is executing in 64-bit, the same LongPtr variable occupies 8 bytes. Therefore, change the datatype of handle argument parameters in Local External Function declarations from ULong (or Long) to LongPtr, and use variables of type LongPtr when you need the value of a handle in PowerScript or in a PB structure that will be passed to Windows. The 64-bit integer data type LongLong may also be used.

4. Memory addresses occupy 8 bytes in 64-bit processes. Since PB doesn't support an address datatype, there's pretty much nothing to see here. However, when the REF keyword precedes a parameter in a Local External Function declaration, take note and understand that under the covers, PB is passing the address of the argument value to Windows...so thankfully, the PB compiler manages this 4-byte/8-byte address concern for us. Keep 8-byte boundary concerns in mind when values are passed by reference.

5. Structures. Structure members that will contain a Windows handle also need to be declared of type LongPtr. In addition, a LongPtr value in memory MUST be aligned on an 8-byte boundary (the memory address of the variable must be evenly divisible by 8). Depending on the structure, some "padding" members may be needed to ensure this condition is met. You can safely assume that the memory allocated to an instance of a structure begins on an 8-byte boundary. These differences imply that the 64-bit version of a structure passed into or out of Window may be different than the 32-bit version.

6. Structures, part 2: Some Windows structures use a member (usually the first member of the structure) to contain the size, in bytes, of the structure. Since structures for 64-bit processes may differ in composition from the 32-bit versions, the length of the structure may differ depending on the bitness. An additional, but perhaps lesser known condition is that in some cases, Windows requires the structure length in a 64-bit process to be evenly divisible by 8. This may require the addition of one or more "reserved" structure-padding members to the end of the structure.

Note: An excellent resource is Roland Smith's TopWizProgramming web site, where he provides dozens of free PB code examples, most of which call WinAPI functions. I know Roland has tweaked many of his examples so they will work as a 64-bit app. Unfortunately, in your particular case, it appears he does not have an example that calls the WinAPI functions that your app is using. You may be able to find sample code in other languages on the web that calls the WinAPI functions you are interested in, such as Visual Basic.NET, that may provide clues you can use. One night last week I found a code sample in a language I did not recognize, but I could understand the syntax well enough that it gave me the hint I needed to solve a 64-bit WinAPI migration I had struggled with for many evenings.

These are the tips, suggestions, and recommendations I've borrowed or discovered over many evenings of experimentation with calling WinAPI functions from a 64-bit PB application. I hope you and maybe other PB developers find this information helpful.

John

Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 27 August 2019 18:29 PM UTC
  2. PowerBuilder
  3. # 2

Hi Balaji;

   Those look like very old ANSI based external functions. Modern PB versions are Unicode. I would try using the Unicode declarations with PB2017/2019 instead ...

WNetUseConnectionA     =>  WNetUseConnectionW

https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetuseconnectionw

WNetCancelConnectionA  => WNetCancelConnectionW

https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetcancelconnectionw

HTH

Regards ... Chris

 

 

Comment
  1. John Fauss
  2. Tuesday, 27 August 2019 18:47 PM UTC
With all due respect, Chris, ANSI/Unicode is not the issue here. Calling WinAPI functions from a 64-bit executable is the problem. All handle arguments are 8 bytes long instead of 4, structure member alignment and length issues, etc. This is why I STRONGLY recommend Balaji stay with 32-bit executable for his application.



Because the PB IDE is 32-bit, there is NO good way to trouble-shoot/debug this kind of issue. I can provide some tips, but I'll have to wait until tonight before I have time to do so. I wish Appeon would do a better job of explaining the issues regarding interfacing to WinAPI calls in 64-bit, including examples. This can be a frustratingly difficult issue to overcome.



John
  1. Helpful
  1. Chris Pollach @Appeon
  2. Tuesday, 27 August 2019 18:53 PM UTC
Hi John;

Correct ... for 64 bits you would have to use the new LongLong or LongPTR data types. Then there is the "progma_pack" challenge as well sometimes.

Regards ... Chris
  1. Helpful
There are no comments made yet.
Balaji Pinjala Accepted Answer Pending Moderation
  1. Tuesday, 27 August 2019 16:14 PM UTC
  2. PowerBuilder
  3. # 3

FUNCTION ulong WNetUseConnectionA(ulong hwndOwner, &
REF s_netresource lpNetResource, string lpPassword, &
string lpUsername, ulong dwFlags, REF string lpAccessName, &
REF ulong lpBufferSize, REF ulong lpResult) library "mpr.dll" alias for "WNetUseConnectionA;Ansi"

FUNCTION ulong WNetCancelConnectionA(REF string lpName, &
boolean fForce) library "mpr.dll" alias for "WNetCancelConnectionA;Ansi"

Comment
There are no comments made yet.
Balaji Pinjala Accepted Answer Pending Moderation
  1. Tuesday, 27 August 2019 14:54 PM UTC
  2. PowerBuilder
  3. # 4

 

Hi we are using Power Builder 2017 R2 version and please find below the syntax we used to connect and disconnect. Thanks. 

 

Syntax used to connect:

ll_ErrInfo = WNetUseConnectionA(ll_null, lstr_netresource, &
is_user_pwd,is_username, &
CONNECT_TEMPORARY, ls_buffer, ll_bufferlen, ll_success)

 

Syntax used to disconnect:

ll_ErrInfo = WNetCancelConnectionA(ls_buffer,bForce)

 

Comment
  1. John Fauss
  2. Tuesday, 27 August 2019 15:35 PM UTC
We need to see the declarations in the Local External Functions tab, not the PowerScript code that executes.. The declarations describes in detail how PB interfaces to each WinAPI function. These declarations will begin with:



Function WNetUseConnectionA( . . . . . ) Library "mpr.dll"

-and-

Function WNetCancelConnectionA( . . . . . ) Library "mpr.dll"
  1. Helpful
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Tuesday, 27 August 2019 14:14 PM UTC
  2. PowerBuilder
  3. # 5

Hi, Balaji -

Please tell us (a) the version of PB you are using and (b) supply the exact syntax for the local external function declarations of the two WinAPI functions your app is calling.

Did you review/update the external function declarations when you switched to producing a 64-bit app? There are critical considerations when using some WinAPI functions from a 64-bit application. If there is no overriding reason to create a 64-bit app, my recommendation is to continue to use 32-bit.

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.