How to call WIN32API and other component libraries (.NET).

zhaozj2021-02-16  52

From: http://www.gotdotNet.com

Platform Invoke Samples

This Document Contains The Release Information for The Set of Platform Invoke Samples. You will Find The Following Sections Below:

Sample overview

How To Use Platform Invoke Attributes

How to Marshal Strings

How to Marshal Structures and Unions

How to Marshal Arrays

Miscellaneous

Requirements

Location of the Samples

Building and rainning the samples

·

Sample overview

The platform invoke samples demonstrate how to call functions exported from an unmanaged library: how to declare different types, how to use available attributes to modify default behavior, how to use methods of the Marshal class when needed, and which aspects of garbage collection and threading Could affect results.

Some of the Samples Use Functions Exported from Windows Libraries and Some Use Functions Exported from a Custom Library. Here Is The Summary of The Topics Demonstrated and where it. Sources locate.

How To Use Platform Invoke Attributes

EntryPoint

Renames a function for use in management code.

WinAPIS / CS / MSGBox.cswinapis / VB / MSGBOX.VB

Charset

CHOOSESSESSHOW STRINGS ARE MARSHALED; Also Affects The Function Name Search Criteria.

EXACTSPELING

InDicates WHETHER THE Name of the entry point in the unmanaged dll Should be modified to correspond to the charset value.

Callingconvention

Calls Functions with Varargs or Calls Methods of Exported Classes.

WinAPIS / CS / Printf.cswinapis / VB / Printf.vbcustom / CS / ClassMethods.cscustom / VB / ClassMethods.vb

PreserveSig

Modifies functions That Return HRESULTS.

WinAPIS / CS / CreateObject.cswinapis / VB / CreateObject.vb

SetLasterror

Guarantees That The Error Code Is Saved After a Function Call.

WinAPIS / CS / Errors.cswinapis / VB / Errors.vb

How to Marshal Stringsstring Byval

Passs a string as an in parameter.

WinAPIS / CS / MSGBox.cswinapis / VB / MSGBOX.VB

String as result

Returns a string from unmanaged code.

Custom / cs / strings.cscustom / vb / strings.vb

String byref

Passs a string as an in / out parameter Using StringBuilder.

Winapis / cs / buffers.cswinapis / vb / buffers.vb

String in structure byval

Passs a string in a structure and the structure is an in parameter.

Custom / cs / stucts.cscustom / vb / structs.vb

String in structure byref (char *)

Passes A String In A Structure and The Structure IS An In / Out Parameter. Unmanaged Side Expects a POINTER TO Character Buffer. Size of the Buffer Is Usually Passed As Another Member of The Structure.

Custom / cs / strings.cscustom / vb / strings.vb

String in structure byref (char [])

Passs a string in a structure and the structure is an in / out parameter. Unmanaged Side Expects Embedded Character Buffer.

WinAPIS / CS / OSISINFO.CSWINAPIS / VB / OSINFO.VB

String in class Byval (char *)

Passes a string in a class, which is always an in / out parameter since. Unmanaged side expects a pointer to character buffer.

WinAPIS / CS / OpenFiledlg.cswinapis / VB / OpenFiledlg.vb

String in class byval (char [])

Passs a string in a class, which is always an in / out parameter Since it. Unmanaged Side Expects Embedded CHARACTER BUFFER.

WinAPIS / CS / OSISINFO.CSWINAPIS / VB / OSINFO.VB

Array of strings byval

Paasses an array of strings byval.

Custom / cs / arrays.cscustom / vb / arrays.vb

Array of structures That Contain Strings Byval

Passs an Array of Structures That Contain Strings Byval.

How to Marshal Structures and Unions

Structure ByVal

Passs a structure as an in parameter.

Custom / cs / stucts.cscustom / vb / structs.vb

Structure byrefpasses a structure as an in / out parameter.

WinAPIS / CS / OSISINFO.CSWINAPIS / VB / OSINFO.VB

Class Byval

Copies a class with only integer members as an in / out parameter Like In Managed Case.

WinAPIS / CS / Systime.cswinapis / VB / Systime.vb

Structure with Nested Structure (Flattened)

PROduces a class tructed Structure IN Unmanaged Side. Structure is Flattened in ONE BIG STRUCTURE ON Managed Side.

WinAPIS / CS / Findfile.cswinapis / VB / FindFile.vb

Structure with Nested Structure (not flattened)

Passs a structure with an an embedded structure.

Custom / cs / stucts.cscustom / vb / structs.vb

Structure That Contains Pointer To Another Structure

Passs a structure That Contains a Pointer To Another Structure as a member.

Array of structures what Contain Only Integers ByVal

Passs An Array of Structures That Contain Only Integers as the in parameter. Members of Array Can Be Changed.

Custom / cs / arrays.cscustom / vb / arrays.vb

Array of structures That Contain Integers and strings byref

..........................

Custom / cs / outarrayofstrunts.cscustom / vb / outarrayofstructs.vb

Unions with value Types

Passes a Union With Value Types (Integer and Double).

Custom / cs / unions.cscustom / vb / unions.vb

Unions with Mixed Types

Passes a Union With Mixed Types (Integer and String).

How to Marshal Arrays

Array of Integers ByVal

Passs an array of integers as an in parameter.

Custom / cs / arrays.cscustom / vb / arrays.vb

Array of Integers Byref

Passs an array of integers as an in / out parameter.

2D Array of Integers ByVal

Passing a matrix of integers as an in parameter.

Miscellaneous

Handleref

Presents a case when handleref Needs to be used to prevent garbage collection.winapis / cs / handleref.cswinapis / vb / handleref.vb

Function Pointers

Passes a delegate to an unmanaged function That Expects a function pointer.

Custom / CS / Callback.cscustom / VB / Callback.vb

Void *

Calls a function That Has void * as a parameter.

Custom / cs / void.cscustom / vb / void.vb

Lparam

Uses Gchandle to Pass A Managed Object To Unmanaged Function That Expects LPARAM.

WinAPIS / CS / GCHANDLE.CSWINAPIS / VB / GCHANDLE.VB

STA / MTA

Changes The Default Apartment Settings When an Unmanaged Function Calls Coinitialize.

WinAPIS / CS / ActiveDir.cswinapis / VB / ActiveDir.vb

Requirements

Portions of the Samples Require .NET Framework and Visual C 6.0.

Note: The Compiled DLL File IS Available for Unmanaged Portion of The Sample In Case You Don't have Visual Studio 6.0.

Location of the Samples

The Interop / Pinvoke Directory of the .NET Framework SDK Samples Directory.

For example:

C: / Program Files / Microsoft.Net / Frameworksdk / Samples / Technologies / Interop / Pinvoke

Thase Samples Contain The Following Directory Structure:

Winapis - Platform Invoke Demonstrated with Functions Exported from Windows Libraries / CS - Sources in C # / VB - Sources in vb.net

Custom - Platform Invoke Demonstrate With Functions Exported from Custom Library / CS - Sources in C # / VB - Sources in VB.NET / LIB - Custom Library Source

Building and rainning the samples

To Run these Samples, Follow The Instructions Listed Below.

1. (optional) Open the Pinvokelib.dsw File in The Custom / Lib Directory With Visual Studio 6. Choose Build / Rebuild All from The Menu To Build Library.

2. Go to the Pinvoke Directory and Type Nmake All at the prompt to build all samples.3. Run Any Exe File from a commnd line to see results of calls to unmanaged functions.

转载请注明原文地址:https://www.9cbs.com/read-27763.html

New Post(0)