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 );