PInvoke a function from a DLL compiled in C
I have the following C function
typedef struct ekeycore_ctx_ ekeycore_ctx;
typedef struct ekeycore_enum_ ekeycore_enum;
typedef struct ekeycore_device_ {
char *serial;
char *portname;
char *node;
BOOL present;
BOOL used;
} ekeycore_device;
typedef struct ekeycore_simple_ ekeycore_simple;
typedef enum {
EKEYCORE_OK = 0, /* everything was fine */
EKEYCORE_NOMEM = 1, /* out of memory */
EKEYCORE_NODEVICE = 2, /* no devices are available */
EKEYCORE_TIMEOUT = 3, /* key did not respond to request */
EKEYCORE_FAULTY = 4, /* device has reported faulty/attacked */
EKEYCORE_UNKNOWN = 5 /* unknown error */
} ekeycore_result;
EKEYCORE_API ekeycore_result ekeycore_simple_get(ekeycore_simple *ctx, unsigned char *buff, size_t buffz)
I need to call this function (ekeycore_simple_get) from C#, how wo开发者_如何学运维uld I go about it? How would the variable types above convert?
You're probably asking a bit much of StackOverflow for someone to work all that out for you, but there is good information about marshalling structs on MSDN (see link below)
These are the places I use for reference to P/invoke techniques:
- http://msdn.microsoft.com/en-us/library/ms384329%28VS.71%29.aspx
- http://www.pinvoke.net/ (very mixed quality)
- The .NET framework source - much better than pinvoke.net for common Windows DLL signatues
精彩评论