I have 2 C++ DLLs. One of them contains the following function:
我有2个C ++ DLL。其中一个包含以下功能:
void init(const unsigned char* initData, const unsigned char* key)
The other one contains this function:
另一个包含此功能:
BYTE* encrypt(BYTE *inOut, UINT inputSize, BYTE *secretKey, UINT secretKeySize).
Is there a way to call these 2 functions from C#? I know you can use [DllImport] in C# to call C++ functions, but the pointers are giving me a hard time.
有没有办法从C#调用这两个函数?我知道你可以在C#中使用[DllImport]来调用C ++函数,但指针给了我很多时间。
Any help would be appreciated!
任何帮助,将不胜感激!
4 个解决方案
#1
Yes, you can call both of these from C# assuming that they are wrapped in extern "C" sections. I can't give you a detailed PInvoke signature because I don't have enough information on how the various parameters are related but the following will work.
是的,您可以从C#中调用这两个,假设它们包含在extern“C”部分中。我不能给你一个详细的PInvoke签名,因为我没有足够的信息来说明各种参数是如何相关的,但以下内容是可行的。
[DllImport("yourdllName.dll")]
public static extern void init(IntPtr initData, IntPtr key);
[DllImport("yourdllName.dll")]
public static extern IntPtr encrpyt(IntPtr inout, unsigned inuputSize, IntPtr key, unsigned secretKeySize);
Pieces of information that would allow us to create a better signature
允许我们创建更好签名的信息
- Is the return of encrypt allocated memory?
- If #1 is true, how is the memory allocated
- Can you give a basic description on how the parameters work?
- I'm guessing that all of the pointer values represents arrays / groups of elements instead of a single element correct?
加密分配内存是否返回?
如果#1为真,则如何分配内存
你能给出一些关于参数如何工作的基本描述吗?
我猜所有指针值都代表数组/元素组而不是单个元素正确吗?
#2
[DllImport("yourdll.dll")]
static extern void init([MarshalAs(UnmanagedType.LPArray)] byte[] initData, [MarshalAs(UnmanagedType.LPArray)] byte[] key);
[DllImport("yourdll.dll")]
static extern IntPtr encrypt([MarshalAs(UnmanagedType.LPArray)] byte[] inOut, int inputSize, [MarshalAs(UnmanagedType.LPArray)] byte[] key, int secretKeySize);
#3
For classes, you don't need to do anything special. For value types, you need to use the ref keyword.
对于课程,您不需要做任何特殊的事情。对于值类型,您需要使用ref关键字。
MSDN has an article that summarizes this: http://msdn.microsoft.com/en-us/library/awbckfbz.aspx
MSDN有一篇文章总结了这一点:http://msdn.microsoft.com/en-us/library/awbckfbz.aspx
#4
For pointers, what you want to use is IntPtr.
对于指针,您要使用的是IntPtr。
[DllImport("whatever.dll")]
static extern void init(IntPtr initData, IntPtr key);
#1
Yes, you can call both of these from C# assuming that they are wrapped in extern "C" sections. I can't give you a detailed PInvoke signature because I don't have enough information on how the various parameters are related but the following will work.
是的,您可以从C#中调用这两个,假设它们包含在extern“C”部分中。我不能给你一个详细的PInvoke签名,因为我没有足够的信息来说明各种参数是如何相关的,但以下内容是可行的。
[DllImport("yourdllName.dll")]
public static extern void init(IntPtr initData, IntPtr key);
[DllImport("yourdllName.dll")]
public static extern IntPtr encrpyt(IntPtr inout, unsigned inuputSize, IntPtr key, unsigned secretKeySize);
Pieces of information that would allow us to create a better signature
允许我们创建更好签名的信息
- Is the return of encrypt allocated memory?
- If #1 is true, how is the memory allocated
- Can you give a basic description on how the parameters work?
- I'm guessing that all of the pointer values represents arrays / groups of elements instead of a single element correct?
加密分配内存是否返回?
如果#1为真,则如何分配内存
你能给出一些关于参数如何工作的基本描述吗?
我猜所有指针值都代表数组/元素组而不是单个元素正确吗?
#2
[DllImport("yourdll.dll")]
static extern void init([MarshalAs(UnmanagedType.LPArray)] byte[] initData, [MarshalAs(UnmanagedType.LPArray)] byte[] key);
[DllImport("yourdll.dll")]
static extern IntPtr encrypt([MarshalAs(UnmanagedType.LPArray)] byte[] inOut, int inputSize, [MarshalAs(UnmanagedType.LPArray)] byte[] key, int secretKeySize);
#3
For classes, you don't need to do anything special. For value types, you need to use the ref keyword.
对于课程,您不需要做任何特殊的事情。对于值类型,您需要使用ref关键字。
MSDN has an article that summarizes this: http://msdn.microsoft.com/en-us/library/awbckfbz.aspx
MSDN有一篇文章总结了这一点:http://msdn.microsoft.com/en-us/library/awbckfbz.aspx
#4
For pointers, what you want to use is IntPtr.
对于指针,您要使用的是IntPtr。
[DllImport("whatever.dll")]
static extern void init(IntPtr initData, IntPtr key);