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里的数据没变,现在不知道怎么写这个函数了,请大大们帮忙看看,谢谢了.
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里的数据没变,现在不知道怎么写这个函数了,请大大们帮忙看看,谢谢了.
...全文
相关推荐
关于C#客户端引用C++ dll的问题
"这篇文章主要介绍了如何在C# Winform客户端项目中引用C++编译的DLL,包括制作可引用的类库以及解决客户端引用时遇到的问题。"在C#应用程序中引用C++编写的DLL,通常涉及到跨语言互
C#动态引用DLL范例
本文将围绕"C#动态引用DLL范例"这一主题,深入探讨如何解决在使用`GetProcAddress`获取方法入口时遇到的问题。首先,我们需要了解动态链接库(DLL)的基本概念。
c#写的日历插件,已生成dll
标题提到的"C#写的日历插件,已生成dll",意味着开发者已经完成了日历功能的编码,并成功地将它打包成DLL文件。DLL文件通常包含了类、函数和其他程序元素,可以供其他项目引用和调用。
C# 引用不同文件夹下的Dll文件
在C#编程中,管理大量的DLL文件确实可能会导致项目结构混乱,降低代码可维护性。为了解决这个问题,我们可以将DLL文件按照功能或模块划分到不同的文件夹中,然后在项目中引用这些位于不同文件夹下的DLL。
C#引用C++动态库 dll 实例
引用C++DLL: 将`CPlusPlusDLL.dll`添加到C#项目的“引用”中,右键点击项目,选择“添加引用”,然后在“浏览”选项卡中找到DLL文件。6.


