c#调用c++ dll,导出函数参数是结构体体数组,DLL已经写好了 ,C#有的地方不知道怎么写,请高手指教! 20
c++代码#include"stdafx.h"#include"string.h"#include"stdio.h"#defineDLLEXPORTextern"C"__...
c++代码
#include "stdafx.h"
#include "string.h"
#include "stdio.h"
#define DLLEXPORT extern "C" __declspec(dllexport)
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
typedef struct Student
{
char name[20];
int age;
double scores[32];
}Student;
typedef struct Class
{
int number;
Student students[126];
}Class1;
DLLEXPORT int GetClass(Class1 pClass[50])
{
for(int i=0;i<50;i++)
{
pClass[i].number =i;
for(int j=0;j<126;j++)
{
memset(pClass[i].students[j].name,0,20);
sprintf(pClass[i].students[j].name,"name_%d_%d",i,j);
pClass[i].students[j].age =j%2==0?15:20;
}
}
return 0;
}
C#代码
[StructLayout(LayoutKind.Sequential)]
struct Student
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public string name;
public int age;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public double[] scores;
}
[StructLayout(LayoutKind.Sequential)]
struct Class1
{
[MarshalAs(UnmanagedType.U1)]
public byte number;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 126)]
public Student [] students;
}
[DllImport("tet2.dll")]
static extern int GetClass( ref Class1 [] tempClass );
static void Main(string[] args)
{
Class1[] temp1=new Class1[50];
GetClass(ref temp1);
}
问题是C#调用 这快不知道怎么写 ,请指点 谢谢~-~ 展开
#include "stdafx.h"
#include "string.h"
#include "stdio.h"
#define DLLEXPORT extern "C" __declspec(dllexport)
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
typedef struct Student
{
char name[20];
int age;
double scores[32];
}Student;
typedef struct Class
{
int number;
Student students[126];
}Class1;
DLLEXPORT int GetClass(Class1 pClass[50])
{
for(int i=0;i<50;i++)
{
pClass[i].number =i;
for(int j=0;j<126;j++)
{
memset(pClass[i].students[j].name,0,20);
sprintf(pClass[i].students[j].name,"name_%d_%d",i,j);
pClass[i].students[j].age =j%2==0?15:20;
}
}
return 0;
}
C#代码
[StructLayout(LayoutKind.Sequential)]
struct Student
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public string name;
public int age;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public double[] scores;
}
[StructLayout(LayoutKind.Sequential)]
struct Class1
{
[MarshalAs(UnmanagedType.U1)]
public byte number;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 126)]
public Student [] students;
}
[DllImport("tet2.dll")]
static extern int GetClass( ref Class1 [] tempClass );
static void Main(string[] args)
{
Class1[] temp1=new Class1[50];
GetClass(ref temp1);
}
问题是C#调用 这快不知道怎么写 ,请指点 谢谢~-~ 展开
展开全部
关键是 lpOnRecvCardEvent 这个函数的第二个参数是个 C++ 指针。
楼主在 C# 中声明这个导出函数时,使用了并不对应的 CardEvent[] C# 数组类型。
正确的做法是: 在 C# 声明 lpOnRecvCardEvent 时,第二个参数应该是 IntPtr ,不是数组。
由于楼主的这个 VC DLL 可能是自己写的,我没办法给出正确代码。
我提供一份模拟代码做参考:
----------------------------------------------------------------------------------------------------
VC DLL:
StructMod.h
#pragma once
#include <Windows.h>
#ifndef STRUCTMOD_EXPORT
#define STRUCTMOD_EXPORT extern "C" __declspec(dllimport)
#endif
typedef struct _tagCardEvent {
DWORD CardNo;
INT32 EvtYear;
} CardEvent, *PCardEvent;
STRUCTMOD_EXPORT CardEvent;
STRUCTMOD_EXPORT PCardEvent;
typedef void CALLBACK PFNTEST(PCardEvent pce, INT32 nEvt);
STRUCTMOD_EXPORT void Caller(PFNTEST pfnTest);
StructMod.cpp
#define STRUCTMOD_EXPORT extern "C" __declspec(dllexport)
#include "StructMod.h"
#include <tchar.h>
void Caller(PFNTEST pfnTest) {
CardEvent ce[2] = , };
if (pfnTest) (*pfnTest)(ce, _countof(ce));
}
--------------------------------------------------------------------------------------------------------------------
C# 调用:
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace IntPtrTest
{
class Program
{
static void Main(string[] args)
{
TestD t = new TestD(Test);
Caller(t);
}
[StructLayout(LayoutKind.Sequential)]
public struct CardEvent
{
public uint CardNo;
public Int32 EvtYear;
}
delegate void TestD(IntPtr p, Int32 nEvt);
[DllImport("StructMod", EntryPoint = "Caller", CallingConvention = CallingConvention.Cdecl)]
private static extern void Caller(TestD t);
public static unsafe void Test(IntPtr p, Int32 nEvt)
{
Console.WriteLine("nEvt = " + nEvt.ToString() + Environment.NewLine);
for (int n = 0; n < nEvt; n++)
{
p = p + n * sizeof(CardEvent);
CardEvent ce = (CardEvent)Marshal.PtrToStructure(p, typeof(CardEvent));
Console.WriteLine("[]", n);
Console.WriteLine("CardNo - " + ce.CardNo.ToString());
Console.WriteLine("EvtYear - " + ce.EvtYear.ToString());
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询