高手请进,如何得到字符串的首地址,加急!!!!!!!!!!!!!!!!

wuzy1229 2007-07-31 11:20:06
是这样的,调用一个ocx控件,他的函数定义如下
C语法 :BOOL ShowString(short bank, short XPos, short YPos, short Color, long lpString)

需要得到字符串的首地址在转化成long型传给参数lpString ;
如 :
delphi 是这样得到的
string s="你好,早上好!"
long w= integer(Pchar(s));

c#我查了一下资料程序这么写的
private void button3_Click(object sender, System.EventArgs e)
{
bool bnok =false;
int LedWidth = 384;

string s = "你好,早上好!";



bnok=axCL2005Ocx1.ComInitial(1,38400,1000);

if ( bnok ==true)
{
bnok=axCL2005Ocx1.ShowString(1,Convert.ToInt16(LedWidth-3*16),0,0,sendmessage(s));

}

//axCL2005Ocx1.CloseCL2005();
}
unsafe static int sendmessage(string s)
{


int w =0 ;
//s = "你好,早上好!";

fixed(char*p=s)
{

w = (int)p;
// axCL2005Ocx1ShowString(1,LedWidth-3*16,0,0,(int)*p);


}
return w;


}

调试bnok到 bnok=axCL2005Ocx1.ShowString(1,Convert.ToInt16(LedWidth-3*16),0,0,sendmessage(s));总是为false得不到true
请各位帮忙



...全文
198 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingyuebuyu 2010-05-28
  • 打赏
  • 举报
回复
bool ShowString(short bank, short XPos, short YPos, short Color, IntPtr lpString)


string s = "你好,早上好! ";

IntPtr lpString=System.Runtime.InteropServices.Marshal.StringToHGlobalUni(s);
showjancn 2010-05-28
  • 打赏
  • 举报
回复
方法1:
C语法 :BOOL ShowString(short bank, short XPos, short YPos, short Color, long lpString)
在C#中改成
BOOL ShowString(..., IntPtr lpString)

//应用时
IntPtr lpString= Marshal.AllocHGlobal(n); //民为String的长度,不是个数。 + 1 ;因为C的字符串以\0结尾。

当然最后要释放空间。

//方法2,
直接改 long 为 byte[] 。即 :BOOL ShowString(..., byte[] lpString)
说白了就是存放了字符串的首地址。
然后把string存在 byte[]中。
L_fengrui 2010-05-28
  • 打赏
  • 举报
回复
你的怎么改好的 我也借鉴下
wuzy1229 2007-07-31
  • 打赏
  • 举报
回复
没人回答,自己结贴了
wuzy1229 2007-07-31
  • 打赏
  • 举报
回复
默默等待,希望曙光来临
wuzy1229 2007-07-31
  • 打赏
  • 举报
回复
没人吗,自己顶

110,536

社区成员

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

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

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