![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PB API调用原型 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-7-3 ![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
The tens of thousands of function calls in the Windows environment can be helpful to PowerBuilder users, but documenting them is nearly impossible. After hundreds of user inquiries, Powersoft Technical Support compiled these technical tips to enable PowerBuilder developers to translate standard Microsoft function calls into PowerBuilder syntax, and to empower developers to use any of the external API calls within their owerBuilder environments.. The following information will help you translate and Windows SDK call to a PowerBuilder API function call. It doesn't matter whether you're using PowerBuilder 4.0, 5.0, 6.0, 7.0 or 8.0 but there are important differences between the 16- and 32-bit versions, as we'll discuss below. Step 1: Converting an SDK Call to a PowerBuilder API Call. First you need to get the syntax that wil be converted. This can be obtained from either a Windows API Bible or the MSDN (Microsoft Developers Network). Step 2: Determining Whether it is a Function or a Subroutine. Function calls return a value; subroutines do not. Here is an example of a Microsoft function: Here is an example of a Microsoft subroutine: Step 3: Converting the datatypes from Microsoft to PowerBuilder.
Most of the datatypes are listed above, but some may be missing. When in doubt read the datatype description first. If still unsure, it is usually safe to assume a 16 bit datatype is a "uint" and a 32 bit datatype is a "ulong", since they are the most common. *If the word "Callback appears as a datatype, it cannot be performed by PowerBuilder. Callback routines are functions that are called from within functions. Step 4: Coding the Global/Local External Function: This is a Microsoft function: The BOOL represents a boolean return code, which is translated to "Boolean" in PB for both 16 and 32 bit. A LPCTSTR means a pointer to a string (see step 3). In PowerBuilder, simply use the word "ref" before declaring the string in either 16- or 32-bit platofrms. DWORD is translated to "uint" for 16 bit and "ulong" for 32 bit. An LPVOID indicates that a structure is being used. In PowerBuilder, create a structure, assign it to an instance variable of that structure and pass it by reference. As a result, the following function declarations can be derived: 9
7
3
1
2
3
4
5
4
8
:
PowerBuilder 16 bit: PowerBuilder 32 bit: Note: In the "gotchas" section listed below, you'll get a further explanation why an "A" is appended to the 32-bit function. You'll also find a handy technique to help you locate function calls within the DLLs. Step 5: Creating a Structure In this particular example a structure is needed so you'll need information on what elements are contained within this structure. The MSDN provides this information since the function being called is a Windows function. In the MSDN the structure appears like this: In PB you would go into the structure painter and in this particular case all of these elements would be converted to ULONG. If one of the elements within the structure was a nested structure or callback routine you would not be able to use this function within PB. In that case the only option would be to create a C DLL that makes the function call and call it from PB. Step 6: Scripting the Function Call. Now that you have the function declaration you need to pass it the proper arguments. Taking the function " GetFileVersionInfoA" listed in Step 4 the following script would be needed: First you'll need to declare the datatypes. Keep in mind the variable names do not have to match the function declaration listed in step 4. boolean lb_rtn // Return code Next is the hardest part and that is assigning values to the arguments. This part may require use of the MSDN, API Bible or whatever reference is available that covers the function you are calling. In this particular case the information is contained within the MSDN. The first argument " ls_filename ", should be set to the path and filename of the target file. ls_filename = "c:\windows\calc.exe" // The calculator would be a good file to test against. The second argument "lu_hand" according to the MSDN is ignored. This is probably reserved for a future version of Windows to use. To be safe the argument should be set to null. setnull(lu_hand) The third argument "lu_len" contains the size of the buffer that the information will be returned into. It is critical that the buffer size not be too small or the information may overflow into another part of memory causing a GPF or Dr. Watson error. In this particular case since the structure contains 13 elements that are all ulong, the number 256 should be sufficient to contain the information. 9
7
3
1
2
3
4
5
4
8
:
lu_len = 256 The last argument "lpdata2" is an instance of the structure "lpdata" and it will be populated by the function call. The final script will appear as follows: boolean lb_rtn // Viewing the output ------------------------------------------------------------- Gotcha's to look out for: Error Messages and what they mean: 9
7
3
1
2
3
4
5
4
8
:
Possible causes: 2. Error: Error calling external function <function_name> at line <line_number> in the <event name> event of object <object_name> of <window_name>. This is probably the result of an incorrectly spelt function name. Be sure to verify that the function name matches what is in the DLL exactly, including the case of the letters. 3. Error: Specified argument type differs from required argument type at runtime in DLL function <function_name>. (Invalid stack pointer on return from function call) at line <line_number> in <event_name> event of object <object_name> of <window_name>. This error usually indicates the datatypes do not match what the DLL function is expecting. 4. PB050: Caused an Invalid Page Fault in module PBSHR050.DLL @ 0137:1111163e This error can occur either immediately upon calling the function or when the application closes. The module and memory address may vary but the reason for this is usually the same. If PB is receiving a string and memory isn't allocated in advance using the SPACE( ) that string will overflow into another memory area. To allocate 12 characters to the string "ls_filename" the following code would be used. ls_filename = space(13) // You may want to give it an extra space just to be safe. 5. Receiving garbage from the DLL. i.e. Last name populated as: "*#^&Ryan" This problem is most likely the result of the byte alignment being set incorrectly. PowerBuilder expects the byte alignment to be set to one and if you are using MSVC++ compiler the default setting is four. To set the byte alignment to one you would need to do the following prior to compiling to a dll. - Select the desired target The DOS switch to set the byte alignment to one is: /zp1 A handy trick to find functions quickly: 1. On Win95,Win98 or NT 4.0 click on the START button and select "Find", then "Files or Folders". Related Faxlines: 9
7
3
1
2
3
4
5
4
8
:
44545 - 32 Bit - Windows API Calls for PowerBuilder 5.0 44648 - Prototyping API Calls for PowerBuilder 4.0, 5.0 and 6.0 44588 - Dynamically Closing a Non-PowerBuilder Application 47703 - GPF's and The PowerBuilder Memory Defragger 47704 - Windows 3.10 and 3.11 Functions - Krnl386.exe, User.exe, Gdi.exe 47707 - 16 Bit - Win95 and NT Functions - Krnl386.exe, User.exe, Gdi.exe 47705 - 32 Bit - Win95 and NT Functions - Kernel32.dll, User32.dll, Gdi32.dll 44474 - External Function Calls 44538 - Passing a 32-bit PowerBuilder structure to a 32-bit C DLL Created in Power++ 9
7
3
1
2
3
4
5
4
8
:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 文章录入:admin 责任编辑:admin | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|