当前位置:编程学习 > C#/ASP.NET >>

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

是这样的,调用一个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
请各位帮忙



--------------------编程问答-------------------- 没人吗,自己顶 --------------------编程问答-------------------- 默默等待,希望曙光来临 --------------------编程问答-------------------- 没人回答,自己结贴了 --------------------编程问答-------------------- 你的怎么改好的 我也借鉴下 
--------------------编程问答-------------------- 方法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[]中。
--------------------编程问答-------------------- bool ShowString(short   bank,   short   XPos,   short   YPos,   short   Color,   IntPtr lpString) 


string   s   =   "你好,早上好! "; 
 
IntPtr lpString=System.Runtime.InteropServices.Marshal.StringToHGlobalUni(s);
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,