S
Stanly Fan
Guest
Hi Dear,
Recently, I have encountered some confusion when I try to call C language interface to C#, I need your help please.
The following is C language head file:
MyTest.h:
#pragma once
#define MYTEST_API __declspec(dllimport)
#if defined (__cplusplus)
extern "C" {
#endif
typedef enum _MyTestErrorCode
{
ERROR0 = 0,
ERROR1,
} MyTestErrorCode;
typedef struct _MyTestLocation
{
int left;
int top;
} MyTestLocation;
typedef enum _MyTestEmotion
{
Emotion0 = 0,
Emotion1 = 1,
} MyTestEmotion;
typedef struct _MyTestAttributes
{
int width;
int height;
char* MyImageBytes; //image bytes
MyTestEmotion myTestEmotion;
MyTestLocation myTestLocation;
} MyTestAttributes;
MYTEST_API MyTestErrorCode MyTest_detect(
void* myTestHandle,
int* num,
MyTestAttributes* myTestAttributes);
#if defined (__cplusplus)
}
#endif
The following is the C# code to import the C language dll.
MyTestCSharp.cs
using System;
using System.Runtime.InteropServices;
namespace MyTest
{
class MyTestCSharp
{
public enum MyTestErrorCode : int
{
ERROR0 = 0,
ERROR1,
}
[StructLayout(LayoutKind.Sequential)]
public struct MyTestLocation
{
public int left;
public int top;
}
public enum MyTestEmotion : int
{
Emotion0 = 0,
Emotion1,
}
[StructLayout(LayoutKind.Sequential)]
public struct MyTestAttributes
{
public int width;
public int height;
//Question1
//How do I transform here from [char*] within C language to C# ?????
public IntPtr MyImageBytes; //char* MyImageBytes;
public MyTestEmotion myTestEmotion;
public MyTestLocation myTestLocation;
}
public static class MyTestHelper
{
[DllImport("MyTest.dll", CharSet = CharSet.Ansi)]
public extern static MyTestErrorCode MyTest_detect(
IntPtr myTestHandle,
ref int num,
//Question2
//How do I transform here from [struct*] within C langusge to C# ?????
//ref MyTestAttributes myTestAttributes); // MyTestAttributes* myTestAttributes
}
}
}
Now I have two questions:
Q1, How do I transform [char*] in C language to C#?
From:
typedef struct _MyTestAttributes
{
int age;
char* MyImageBytes; //image bytes
MyTestEmotion myTestEmotion;
MyTestLocation myTestLocation;
} MyTestAttributes;
To:
[StructLayout(LayoutKind.Sequential)]
public struct MyTestAttributes
{
public readonly int age;
//Question1
//How do I transform here from [char*] within C language to C# ?????
public IntPtr MyImageBytes; //char* MyImageBytes;
public MyTestEmotion myTestEmotion;
public MyTestLocation myTestLocation;
}
[char*] in C language is an unmanaged type, I try to use IntPtr in C#, however, there is a problem that it seems the IntPtr can not get the memory length of the stored binary photo stream which I need to know, so do you have any idea here?
Q2, How do I transform [struct*] which including another struct and enum in C language to C#?
From:
MYTEST_API MyTestErrorCode MyTest_detect(
void* myTestHandle,
int* num,
MyTestAttributes* myTestAttributes);
To:
public static class MyTestHelper
{
[DllImport("MyTest.dll", CharSet = CharSet.Ansi)]
public extern static MyTestErrorCode MyTest_detect(
IntPtr myTestHandle,
ref int num,
//Question2
//How do I transform here from [struct*] within C language to C# ?????
//ref MyTestAttributes myTestAttributes); // MyTestAttributes* myTestAttributes
}
How do I transform [struct*] which including another struct and enum in C language to C#?
[struct*] in C language is also an unmanaged type, and stand for an unmanaged structural array here with a fixed array length, also I need to use more than one item within the struct, how can I define the return type in C# here?
I try to return an array, but it seems throw error when I call it:
public static class MyTestHelper
{
[DllImport("MyTest.dll", CharSet = CharSet.Ansi)]
public extern static MyTestErrorCode MyTest_detect(
IntPtr myTestHandle,
ref int num,
//I try to return an array, but it seems throw error when I call it
ref MyTestAttributes[] myTestAttributes); // MyTestAttributes* myTestAttributes
}
Thanks for any kind advice.
Regards,
Starry
Continue reading...
Recently, I have encountered some confusion when I try to call C language interface to C#, I need your help please.
The following is C language head file:
MyTest.h:
#pragma once
#define MYTEST_API __declspec(dllimport)
#if defined (__cplusplus)
extern "C" {
#endif
typedef enum _MyTestErrorCode
{
ERROR0 = 0,
ERROR1,
} MyTestErrorCode;
typedef struct _MyTestLocation
{
int left;
int top;
} MyTestLocation;
typedef enum _MyTestEmotion
{
Emotion0 = 0,
Emotion1 = 1,
} MyTestEmotion;
typedef struct _MyTestAttributes
{
int width;
int height;
char* MyImageBytes; //image bytes
MyTestEmotion myTestEmotion;
MyTestLocation myTestLocation;
} MyTestAttributes;
MYTEST_API MyTestErrorCode MyTest_detect(
void* myTestHandle,
int* num,
MyTestAttributes* myTestAttributes);
#if defined (__cplusplus)
}
#endif
The following is the C# code to import the C language dll.
MyTestCSharp.cs
using System;
using System.Runtime.InteropServices;
namespace MyTest
{
class MyTestCSharp
{
public enum MyTestErrorCode : int
{
ERROR0 = 0,
ERROR1,
}
[StructLayout(LayoutKind.Sequential)]
public struct MyTestLocation
{
public int left;
public int top;
}
public enum MyTestEmotion : int
{
Emotion0 = 0,
Emotion1,
}
[StructLayout(LayoutKind.Sequential)]
public struct MyTestAttributes
{
public int width;
public int height;
//Question1
//How do I transform here from [char*] within C language to C# ?????
public IntPtr MyImageBytes; //char* MyImageBytes;
public MyTestEmotion myTestEmotion;
public MyTestLocation myTestLocation;
}
public static class MyTestHelper
{
[DllImport("MyTest.dll", CharSet = CharSet.Ansi)]
public extern static MyTestErrorCode MyTest_detect(
IntPtr myTestHandle,
ref int num,
//Question2
//How do I transform here from [struct*] within C langusge to C# ?????
//ref MyTestAttributes myTestAttributes); // MyTestAttributes* myTestAttributes
}
}
}
Now I have two questions:
Q1, How do I transform [char*] in C language to C#?
From:
typedef struct _MyTestAttributes
{
int age;
char* MyImageBytes; //image bytes
MyTestEmotion myTestEmotion;
MyTestLocation myTestLocation;
} MyTestAttributes;
To:
[StructLayout(LayoutKind.Sequential)]
public struct MyTestAttributes
{
public readonly int age;
//Question1
//How do I transform here from [char*] within C language to C# ?????
public IntPtr MyImageBytes; //char* MyImageBytes;
public MyTestEmotion myTestEmotion;
public MyTestLocation myTestLocation;
}
[char*] in C language is an unmanaged type, I try to use IntPtr in C#, however, there is a problem that it seems the IntPtr can not get the memory length of the stored binary photo stream which I need to know, so do you have any idea here?
Q2, How do I transform [struct*] which including another struct and enum in C language to C#?
From:
MYTEST_API MyTestErrorCode MyTest_detect(
void* myTestHandle,
int* num,
MyTestAttributes* myTestAttributes);
To:
public static class MyTestHelper
{
[DllImport("MyTest.dll", CharSet = CharSet.Ansi)]
public extern static MyTestErrorCode MyTest_detect(
IntPtr myTestHandle,
ref int num,
//Question2
//How do I transform here from [struct*] within C language to C# ?????
//ref MyTestAttributes myTestAttributes); // MyTestAttributes* myTestAttributes
}
How do I transform [struct*] which including another struct and enum in C language to C#?
[struct*] in C language is also an unmanaged type, and stand for an unmanaged structural array here with a fixed array length, also I need to use more than one item within the struct, how can I define the return type in C# here?
I try to return an array, but it seems throw error when I call it:
public static class MyTestHelper
{
[DllImport("MyTest.dll", CharSet = CharSet.Ansi)]
public extern static MyTestErrorCode MyTest_detect(
IntPtr myTestHandle,
ref int num,
//I try to return an array, but it seems throw error when I call it
ref MyTestAttributes[] myTestAttributes); // MyTestAttributes* myTestAttributes
}
Thanks for any kind advice.
Regards,
Starry
Continue reading...