首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

指针的有关问题!

2013-08-16 
指针的问题!!!!!!我遇到一个问题,详细代码如下: [DllImport(LClientDll.dll,EntryPointLClientStartDe

指针的问题!!!!!!
我遇到一个问题,详细代码如下:
 [DllImport("LClientDll.dll",EntryPoint="LClientStartDemo",ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]
        public static extern unsafe int LClientStartDemo(string host, string port, string oper, string ch, int flag);
        [DllImport("LClientDll.dll",EntryPoint="LClientRcvDemo",ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]
        public static extern unsafe int LClientRcvDemo(char *data);
        [DllImport("LClientDll.dll",EntryPoint="LClientStop",ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]
        public static extern int  LClientStop();
        public Form1()
        {
            InitializeComponent();
            

        }

        private void button1_Click(object sender, EventArgs e)
        { 
            
        
            int r = LClientStartDemo("127.0.0.1", "7628", "801", "0", 0);
            
           
           
        }
LClientRcvDemo(char *data)参数是指针我希望将值传递给一个变量然后进行进一步处理方案可行吗?具体如何操作?
[解决办法]


[DllImport("LClientDll.dll")]
    public static extern int LClientStartDemo(string host, string port, string oper, string ch, int flag);


    [DllImport("LClientDll.dll")]
    public static extern int LClientRcvDemo(StringBuilder sb);

private void button1_Click(object sender, EventArgs e)
{
int r = LClientStartDemo("127.0.0.1", "7628", "801", "0", 0);
while (true)
{
StringBuilder str = new StringBuilder(100);
r = LClientRcvDemo(str);
Console.WriteLine("r={0}", r);
if (r == -100) continue;
if (r > 0)
{
MessageBox.Show(string.Format("r={0} data=[{1}]", r, str));
}
else
{
MessageBox.Show(string.Format("r={0} restart...", r));
break;
}
}
}


[解决办法]
修改方法签名     [DllImport("LClientDll.dll",EntryPoint="LClientRcvDemo",ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]
        public static extern unsafe int LClientRcvDemo(char *data);

unsafe去掉
char *dat 改为 string data
[解决办法]
那要看你data是什么类型的指针参数,如果是对象的话可以用(Intptr data),
然后转换得到Object ob; Marshal.PtrToStructure(data, ob);
[解决办法]
对于字符串的处理,最好把CharSet的影响考虑进去。
 [DllImport("*.dll", CharSet = CharSet.Ansi)]
传入参数使用String,传出使用StringBuilder,StringBuilder使用前要分配空间。

热点排行