c#中dll引用问题
有一个fnthex32.dll 查看不到里边函数GETFONTHEX的参数了,只有一个dephi的例子可以正确执行,我想把这个方法用C#实现,出现了访问内存错误和运行库错误.下面是dephi里的代码
function GETFONTHEX(chnstr: string; fontname: string; orient: integer; height: integer; width: integer; bold: integer; italic: integer; hexbuf: string): integer; stdcall; external 'fnthex32.dll';
function PrtChnStr(x, y: integer; fontname: string; height, xmf, ymf: integer; chnstr: string): string;
var
buf, ret: string;
count: integer;
begin
result := '';
setlength(buf, 21 * 1024);
count := GETFONTHEX(chnstr, fontname, 0, height, 0, 1, 0, buf);
if count > 0 then
begin
ret := Copy(buf, 1, count);
result := ret;
end;
end;
我贴上我用C#改的
[DllImport("fnthex32.dll",CharSet = CharSet.Ansi)]
public static extern int GETFONTHEX(
string chnstr,
string fontname,
int orient,
int height,
int width,
int bold,
int italic,
ref string hexbuf);
调用:
private string GetCharset()
{
char []strHex = new char[21*1024];
string test = new string(strHex); ;
int count;
count = GETFONTHEX( "测试" , "宋体", 0, 5, 30, 0, 0, ref test );
if (count > 0)
{
return test.Substring(0,count)+ "^FO10,140^XGOUTSTR01,1,2^FS";
}
return "";
}
我感觉是 ref string hexbuf参数的问题 ,把ref 去掉可以执行,但返回不了GETFONTHEX里的执行情况,就是test里的数据没变,现在不知道怎么写这个函数了,请大大们帮忙看看,谢谢了.