IntPtr idp= IntPtr.Zero;
StringBuilder idata = new StringBuilder("000000");
string idata ="000000";
我这样建立的2个idata字符串,如何让idp指向他
我指向他的目的是为了传递给dll的某个函数,他需要传指针
还有我定义了一个结构如何向dll的函数传递其指针?
http://community.csdn.net/Expert/topic/5612/5612998.xml?temp=.9482843http://community.csdn.net/Expert/topic/5614/5614773.xml?temp=.5012018
所有分了,解决一起结了。

解决方案 »

  1.   

    char[] charArray = StringBuilder.ToString().ToCharArray();
    unsafe
    {
         fixed(char* p = &charArray[0])
         {
              //p就是字串首地址
         }
    }
      

  2.   

    http://msdn2.microsoft.com/en-us/library/s97shtze(VS.80).aspx
      

  3.   

    //参考标准API如何实现的[DllImport("user32.dll")]
    public static extern int GetWindowText(IntPtr hWnd, 
        StringBuilder lpString, int nMaxCount);private void button1_Click(object sender, EventArgs e)
    {
        StringBuilder vStringBuilder = new StringBuilder(256);
        GetWindowText(Handle, vStringBuilder, vStringBuilder.Capacity);
        Console.WriteLine(vStringBuilder.ToString());
    }
      

  4.   

    用GCHandle.Alloc(object obj)方法来给string分配一个指针!
      

  5.   

    to zswang(伴水清清)(专家门诊清洁工) :
    我想用IntPtr取出当前字符串的指针然后传递到我将要用到的dll函数中,然后这个dll函数会根据这指针对这个StringBuilder 进行读取写入操作,然后返回其值c#环境中进行操作
    ==============================================
    指向StringBuilder 暂时先不管他,因为我用IntPtr idp= IntPtr.Zero;
    这idp是0得值也可以调用该函数,而且这函数返回值我先略过去
    现在我想让IntPtr idp= IntPtr.Zero作为一个结构的指针传递到dll中去又出现问题了。
    问题估计是一种情况,就是我在c#里声明的无论字符串也好结构也好,哪怕传递了ref 进去照样是"写保护"的,dll是无法进行写操作的,我该怎么往里传递呢?
      

  6.   

    能不能把你的DLL拿过来让我看看??
      

  7.   

    public struct CINFTBL
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
        public String htid;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
        public String htdmy;
        public long adv;
        public long fileno;
        public long filesize;
        public long jobcmd;
        public long datatype;
    }这结构
    函数里面需要调用c原形这样 Function :
    After polling, acquire the table data stored the communication management information. When the first data after polling is acquired, call this command. Call hst_getcinfnext by the next programming.
     Form:
    int hst_getcinffirst(CINFTBL *cinftbl)
     Parameter:
    typedef struct CINFTBL { Data acquirement territory for the stored table of the
                                     communication management information.
    UCHAR htid[6] ID letter line
    Set ID letter line of dt700/750 in sending management.
    UCHAR htdmy[2] Dummy
    This command is no meaning.
    long adv Priority Order
    Priority order is set.
    long fileno File number
    Set the sending file number in requesting the sending file from dt700/750.
    long filesize Total of file size
    Set the total sending file in requesting the sending file from dt700/750.
    long jobcmd Management request command
    Set the management command requested from dt700/750 in  establishing the
                         data linking.
    0:file receiving(PC←HT)、1:file sending(PC→HT)、
                        255:cutting request
    long datatype Kind of data
    Set the kind of the objected data requested the management fromdt700/750.
    } cinftbl
      

  8.   

    .net里API调用没有必要考虑字符串指针吧?应该定义为stirng就可以,结构也是如此,只要自定义结构中的元素和API的保持一致就行。你可以参考一下.net框架的基类是如何调用API的,比如常见的事件消息MSG结构是如何定义的,GDI绘图中用到的rect结构是如何定义和使用的。界面API的封装在System.Windows.Forms.UnsafeNaiveMethods类中,需要用反编工具才能查看。
      

  9.   

    这个字符串dll会读取修改并返回给c#的,用string 会被写保护的
      

  10.   

    举个例子
    [DllImport("user32.dll")]
    public static extern IntPtr SendMessage(HandleRef hWnd, int msg, ref int wParam, ref int lParam);
    如果Api方法需要返回这个参数,那么就用ref string paramname来定义参数。
     
      

  11.   

    String s = "Hello";
        IntPtr p = Marshal.StringToHGlobalAnsi(s);