初学C#我用C++ 做了dll其中有个函数
void Convert(string  str[] ) ;
或者
void Convert(string * str ) ;如果可以调用该怎么调用 C#中数组声明和C++不同 好像也没有指针啊
各位懂得帮忙解答下啊

解决方案 »

  1.   

    1、在C#中引用dll文件将MyDll.dll和MyDll.lib拷贝到可执行文件目录下。
    2、添加引用
    3、
    [DllImport("XXXX.dll", ntryPoint="Convert",  CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] // 注意:引号内必须为完整的函数名,不能有空格 public static extern int Convert(string  str[]); 
      
    使用如下语句调用:
    RefComm.Convert(str[]); 
      

  2.   

        [DllImport("t2.dll")]
            public static extern void GetString(string str);
      

  3.   

     [DllImport("t2.dll")]
            public static extern void GetString(StringBuilder str); 
      

  4.   

    借楼主的地盘,问问我的问题,谢谢我现在用的是.net1.1开发的网站!因为服务器的原因,所以只能用.net1.1。但是这就让我遇到一个问题!
    在我的网页中需要调用一个用c语言的写的dll(注:在vs2008中测试一切ok)
    我用LoadLibrary得到dll句柄,然后用GetProcAddress得到函数的指针,在.Net2.0,.Net3.5下,我可以通过类似于下面的代码
    IntPtr api = GetProcAddress(hLib, APIName);
            return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
    来得到一个一个委托,从而得到dll中的函数但是不幸的是,在.Net1.1还不支持这个函数,请问如何做,谢谢!
      

  5.   

    那你为什么不做一个C#的dll呢?直接添加引用,再using一下不就可以了么
      

  6.   

    1、在C#中引用dll文件将MyDll.dll和MyDll.lib拷贝到可执行文件目录下。 
    2、添加引用 
    3、 
    public class MyDll
    {
    [DllImport("MyDll.dll",CharSet=CharSet.Auto)]  // 注意:引号内必须为完整的函数名,不能有空格  public static extern void Convert(string  str[]);  
    }
    使用如下语句调用: 
    MyDll.Convert(str[]);  
      

  7.   

    1.2.3.我知道 关键的问题是
    public static extern int Convert(string  str[]);  这行就会有错误因为C#中声明数组不能string  str[]
      

  8.   

    C#里  string[]  str 这样声明数组的
      

  9.   

    C#默认的是安全代码,你只要将C#设为不安全代码unsafe就可以使用了
      

  10.   

    使用指针参考: public unsafe class Memory
        {
            // Handle for the process heap. This handle is used in all calls to the
            // HeapXXX APIs in the methods below.
            static int ph = GetProcessHeap();
            // Private instance constructor to prevent instantiation.
            private Memory() { }
            // Allocates a memory block of the given size. The allocated memory is
            // automatically initialized to zero.
            public static void* Alloc(int size)
            {
                void* result = HeapAlloc(ph, HEAP_ZERO_MEMORY, size);
                if (result == null) throw new OutOfMemoryException();
                return result;
            }
            // Copies count bytes from src to dst. The source and destination
            // blocks are permitted to overlap.
            public static void Copy(void* src, void* dst, int count)
            {
                byte* ps = (byte*)src;
                byte* pd = (byte*)dst;
                if (ps > pd)
                {
                    for (; count != 0; count--) *pd++ = *ps++;
                }
                else if (ps < pd)
                {
                    for (ps += count, pd += count; count != 0; count--) *--pd = *--ps;
                }
            }
            // Frees a memory block.
            public static void Free(void* block)
            {
                if (!HeapFree(ph, 0, block)) throw new InvalidOperationException();
            }
            // Re-allocates a memory block. If the reallocation request is for a
            // larger size, the additional region of memory is automatically
            // initialized to zero.
            public static void* ReAlloc(void* block, int size)
            {
                void* result = HeapReAlloc(ph, HEAP_ZERO_MEMORY, block, size);
                if (result == null) throw new OutOfMemoryException();
                return result;
            }
            // Returns the size of a memory block.
            public static int SizeOf(void* block)
            {
                int result = HeapSize(ph, 0, block);
                if (result == -1) throw new InvalidOperationException();
                return result;
            }
            // Heap API flags
            const int HEAP_ZERO_MEMORY = 0x00000008;
            // Heap API functions
            [DllImport("kernel32")]
            static extern int GetProcessHeap();
            [DllImport("kernel32")]
            static extern void* HeapAlloc(int hHeap, int flags, int size);
            [DllImport("kernel32")]
            static extern bool HeapFree(int hHeap, int flags, void* block);
            [DllImport("kernel32")]
            static extern void* HeapReAlloc(int hHeap, int flags,
               void* block, int size);
            [DllImport("kernel32")]
            static extern int HeapSize(int hHeap, int flags, void* block);
        }
      

  11.   

    如果
    public static extern int Convert(string[]  str);这样声明就找不到函数路口了 
      

  12.   

     [DllImport("t2.dll")] 
     public static extern void GetString(StringBuilder str); 
    C++中的数组可以看作指针
      

  13.   

    我按照16楼
    [DllImport("ConverDll")]
    private static extern void Convert1(StringBuilder str );
    string[] a = { "wo", "ni" };
    Convert1(a);  //The best overloaded method match for 'TestStringArray.Program.Convert1(System.Text.StringBuilder)' has some invalid argument
                 //Argument '1': cannot convert from 'string[]' to 'System.Text.StringBuilder'
      

  14.   

    [DllImport("ConverDll")] 
    private static extern void Convert1(StringBuilder str ); 
    StringBuilder sb=new StringBuilder();
    sb.Append("wo");
    sb.Append("ni");
    Convert1(sb);  
      

  15.   


    如果dll是你写的就不要用string了,从C#中调用会很麻烦的。你这个string应该是std::string吧,它是一个类,而且是带有引用计数的类,C#里并没有直接对应的东西。把C#设为不安全代码unsafe,调用Heap API等都无助于解决上面的问题。你可以直接用void Convert(LPSTR* str, int length),其实这样反而比较容易一些。
      

  16.   

    楼上说的很有道理
    实际的情况更加复杂void Convert(LPSTR* str, int length) 这样只能获得一个字符串 不能获得一堆字符串偶刚进公司实习就做这么麻烦的事 郁闷啊
      

  17.   

    参照20楼(gomoku)所给的提示 问题终于解决了!! 
    先做个DLl把C++中的string 类型转换成 LPCSTR 作为传递参数
    然后C#中就有了相应的类型  System.String 
    相对应的类型参考这个http://dev.csdn.net/develop/article/28/28164.shtm非常感谢大家的回答啊 结贴去