c#中dll引用问题

genome24 2007-08-25 05:19:23
有一个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里的数据没变,现在不知道怎么写这个函数了,请大大们帮忙看看,谢谢了.
...全文
526 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
doubleu2005 2009-01-10
  • 打赏
  • 举报
回复
return hexbuf.ToString().Substring(0, count) + "^FO10,140^XGOUTSTR01,1,2^FS ";

弄好了就给大家看一下嘛!!!什么大不了的不易!!!!!
Alan_pft 2008-12-31
  • 打赏
  • 举报
回复
我按照6楼的改了,但是如果把 hexbuf 改成StringBuilder后,

char []strHex = new char[21*1024];

string test = new string(strHex);
就不对了,这里是string 才能做的
test.Substring()是string test才有的方法,StringBuilder test是没有的?
恕小弟愚笨,谁能给个完整的代码?因为我的fnthex32.dll是用VB的,用不来,而且里面的参数好象多着你们的一个,多了第3个参数,outstr
Alan_pft 2008-12-31
  • 打赏
  • 举报
回复
终于弄好了,不易啊,实数不易
youIlove1 2008-03-25
  • 打赏
  • 举报
回复
我 也碰到你这样的问题!帮顶!!!!!
希望个位高手给指点哈!
youIlove1 2008-03-20
  • 打赏
  • 举报
回复
#region----------------------------设置汉字-------------------------------------
private string GetCharset()
{

char[] strHex = new char[21 * 1024];

string test = new string(strHex); ;
StringBuilder hexbuf = new StringBuilder(255);
int count;
count = GETFONTHEX("测试 ", "宋体 ", 0, 5, 30, 0, 0, hexbuf);
if (count > 0)
{
return test.Substring(0, count) + "^FO10,140^XGOUTSTR01,1,2^FS ";
}
else return "";
}
#endregion

private void Printbutton_Click(object sender, System.EventArgs e)
{
string strCard;
strCard = this.cardTxt.Text.Trim();
我怎么用了以后会出现一个异常:count = GETFONTHEX("测试 ", "宋体 ", 0, 5, 30, 0, 0, hexbuf);
Avoid 2007-08-26
  • 打赏
  • 举报
回复
ref string hexbuf
改为StringBuilder hexbuf

传入参数前县给StringBuilder 分配内存

StringBuilder hexbuf = new StringBuilder(255);
sowenxiong 2007-08-26
  • 打赏
  • 举报
回复
ref string hexbuf

改成 ref buty [] hexbuf
Avoid 2007-08-26
  • 打赏
  • 举报
回复
因为string是固定的,而StringBuilder 是可变的
gto9999 2007-08-26
  • 打赏
  • 举报
回复
是不是没有名空间的定义?
applethink 2007-08-26
  • 打赏
  • 举报
回复
是不是.net的内存安全机制引起的,hexbuf应该是操作托管堆中的内存地址,跟C++的实际地址不一样
applethink 2007-08-26
  • 打赏
  • 举报
回复
先看看,帮顶一下
genome24 2007-08-26
  • 打赏
  • 举报
回复
问题解决了,先把分给了.ls能说说为什么用stringbulider吗
ccp5780199 2007-08-25
  • 打赏
  • 举报
回复
看不懂DELPHI...
但是REF的话应该就是传进处理后输出吧

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧