110,539
社区成员
发帖
与我相关
我的任务
分享
[DllImport()]
public static extern int FL_GetFea(int nAddr,ref byte[] pTemplet,ret int iTempletLength);
pTemplet, iTempletLength 可能是输入参数。
你看看API,
#include "stdafx.h"
#include <stdio.h>
extern "C" __declspec(dllexport) void GetName(char* buffer, int* size)
{
printf("before copy the length is:%d\n", *size);//写数据前buffer的容量
char temp[] = "hello,world";
strcpy(buffer,temp); //模拟写数据
printf("OK, string is:%s\n", buffer);
*size = (int)strlen(temp);
printf("after copy the length is:%d\n", *size);//写数据后buffer的容量
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ConsoleApplication3
{
class Program
{
[DllImport("MyDll.dll")]
private extern static void GetName(StringBuilder sb, IntPtr length);
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(256);
IntPtr ptr = Marshal.AllocHGlobal(sizeof(int));//申请一个整数指针
Marshal.WriteInt32(ptr, sb.Capacity);//给指针内容赋初值为缓冲区容量
GetName(sb, ptr);//调用Dll函数
//可以在这里测试SB的实际内容/长度,来测试DLL函数是否正确执行
Marshal.FreeHGlobal(ptr);
}
}
}