vo_to_net:pcall
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
vo_to_net:pcall [2018/03/03 05:46] – created wolfgangriedmann | vo_to_net:pcall [2018/07/29 05:22] (current) – wolfgangriedmann | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PCall vs PCallNative ====== | ====== PCall vs PCallNative ====== | ||
- | '' | + | '' |
- | < | + | |
- | You need to know the return type of the called function and change it do PCallNative: | + | |
- | < | + | |
- | If you need source code compatibility to VO, use '' | + | |
- | < | + | |
- | PCallNative<int>( ptrFunction, | + | |
- | #else | + | |
- | PCall( ptrFunction, | + | |
- | # | + | |
+ | The X# compiler needs a typed function pointer for '' | ||
+ | <code visualfoxpro> | ||
+ | RETURN 0 | ||
+ | .... | ||
+ | local hFunc as __GetRealOsVersion ptr | ||
+ | dword( _cast , PCALL( hFunc ,@struOS) )</ | ||
+ | The alternative (to not define such a function) is to use '' | ||
+ | <code visualfoxpro> | ||
+ | PCALLNative< | ||
+ | |||
+ | The .NET way would be the definition of a delegate (i.e. a typed function pointer). Please see [[: | ||
+ | A sample for the delegate version is the following: | ||
+ | <code visualfoxpro> | ||
+ | local oFunc as RtlGetVersionDelegate | ||
+ | |||
+ | oFunc := ( RtlGetVersionDelegate ) Marshal.GetDelegateForFunctionPointer( hFunc, TypeOf( RtlGetVersionDelegate ) ) | ||
+ | oFunc: | ||
+ | |||
+ | To simplify the code, a static method could help: | ||
+ | <code visualfoxpro> | ||
+ | |||
+ | static method CreateDelegate< | ||
+ | local oFunc as T | ||
+ | local oDelegate as object | ||
+ | |||
+ | oDelegate := Marshal.GetDelegateForFunctionPointer( hFunc, TypeOf( T ) ) | ||
+ | |||
+ | oFunc := ( T ) oDelegate | ||
+ | |||
+ | return oFunc | ||
+ | |||
+ | end class</ | ||
+ | and to be used: | ||
+ | <code visualfoxpro> | ||
+ | (Code courtesy of Chris Pyrgas) | ||
+ | |||
+ | The X# development team recommends the use of '' | ||
+ | |||
+ | The following was stated by a member of the development team: | ||
+ | |||
+ | '' | ||
+ | For this reason I would recommend using PCall because the function name used in the declaration gives you full control of the parameter types in the delegate.'' | ||
+ | |||
+ | Please see more details in the X# Forum discussion: | ||
+ | [[https:// | ||
+ | |||
+ | Please note: | ||
+ | |||
+ | '' | ||
+ | |||
+ | '' | ||
+ | |||
+ | The best option to call a function from a dynamically loaded DLL is to define a delegate, assign the function pointer to it and call then the delegate. | ||
+ | |||
+ | In VO, dynamically loading DLLs and executing function from it was interesting to make the application loading times shorter, but in .NET thanks to its lazy loading this is not more necessary. | ||
+ | |||
+ | Executing functions dynamically is only needed when you are not sure that the functionality is installed, or if you are using different versions dependent on some other parameters like OS version or 3rd party DLL version. |
vo_to_net/pcall.1520055975.txt.gz · Last modified: 2018/03/03 05:46 by wolfgangriedmann