请问PansiChar输出是乱码 怎么转成string?

jonhy.luo 2016-11-12 06:19:59
用delphi 写一个dll 其中方法 参数 是Pansichar类型 用Vb调用这个DLL后 把VB传入的 String 写到文件 结果是乱码 请问怎么转换

delphi dll的 代码如下:


library mydll;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
SysUtils,
Classes;

{$R *.res}
function totxt(strs:PansiChar):integer;
var
mystrs: string;
sList: TStrings;
path: string;
begin
mystrs :=StrPas(strs);
sList := TStringList.Create;
path := 'c:\infos.txt';
try
if not DirectoryExists(ExtractFilePath(path)) then
begin
ForceDirectories(ExtractFilePath(path));
end;
if FileExists(path) then
begin
sList.LoadFromFile(path);
end;
sList.Add(FormatDateTime('YYYY-MM-dd', now) +':'+ #13#10 + strs);
sList.SaveToFile(path);
finally
sList.Free;
end;
end;
exports
totxt index 1;

begin
end.


vb 调用代码:

Public Declare Sub totxt Lib "mylib.dll" (ByVal str As String)

totxt ("你好")



结果 是 dll 生成 info.txt 然后里边 传入的str成乱码了 请问怎么解决 谢谢
...全文
486 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jonhy.luo 2016-11-21
  • 打赏
  • 举报
回复
谢谢大家的热情解答 已经解决了 是因为 导出dll 在其他语言调用时候 为了加强兼容必须 使用 stdcall定义函数 感谢 DelphiGuy frtrnr 和 tcmakebest 自己有点忘了 遇到乱码 自己也忘了编码转换 没用Delphi写过dll 只看了函数原来的 原型 也没注意 stdcall 谢谢大家了
  • 打赏
  • 举报
回复
function totxt(strs:PChar):integer; stdcall;
tcmakebest 2016-11-13
  • 打赏
  • 举报
回复
写DLL处理字符串, 最好是UNICODE类型的兼容性好, 其次是 UTF8, 最差就是 ANSISTRING 了. 楼主可以检测一下, VB 是用的哪一种.
frtrnr 2016-11-12
  • 打赏
  • 举报
回复
utf8toansi,或者其他函数,不行的话,你需要拷贝一些乱码来,让大家解析一下。

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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