初学C#我用C++ 做了dll其中有个函数
void Convert(string str[] ) ;
或者
void Convert(string * str ) ;如果可以调用该怎么调用 C#中数组声明和C++不同 好像也没有指针啊
各位懂得帮忙解答下啊
解决方案 »
- 用datatable制作产品订购单录入界面,如何根据代码从数据库自动获取名称的值?
- 如何以圆及圆外一点做一个区域 ?
- 连接数据库的字符串
- 怎么在项目中使用面向对象的知识
- 总是报"没有为任何调用堆栈框架加载任何符号。无法显示源代码",怎么解决啊?请指教
- 关于 多核 程序设计?牛人进
- 为什么数据库中有的表的数据显示为乱码?? 急 在线等
- 在Web窗体设计器未能加载该文件 自定义封送拆收器...不实现ICustomMarshaler接口
- 这要怎么办呢??SOS
- 请问int i=0;与int i= new int();创建的i有什么区别,另外还有。。。。拜托了!
- 存储过程中的事务处理问题
- 100分求 Using …… try…..catch 等推荐的良好编码实践。
2、添加引用
3、
[DllImport("XXXX.dll", ntryPoint="Convert", CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] // 注意:引号内必须为完整的函数名,不能有空格 public static extern int Convert(string str[]);
使用如下语句调用:
RefComm.Convert(str[]);
public static extern void GetString(string str);
public static extern void GetString(StringBuilder str);
在我的网页中需要调用一个用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还不支持这个函数,请问如何做,谢谢!
2、添加引用
3、
public class MyDll
{
[DllImport("MyDll.dll",CharSet=CharSet.Auto)] // 注意:引号内必须为完整的函数名,不能有空格 public static extern void Convert(string str[]);
}
使用如下语句调用:
MyDll.Convert(str[]);
public static extern int Convert(string str[]); 这行就会有错误因为C#中声明数组不能string str[]
{
// 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);
}
public static extern int Convert(string[] str);这样声明就找不到函数路口了
public static extern void GetString(StringBuilder str);
C++中的数组可以看作指针
[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'
private static extern void Convert1(StringBuilder str );
StringBuilder sb=new StringBuilder();
sb.Append("wo");
sb.Append("ni");
Convert1(sb);
如果dll是你写的就不要用string了,从C#中调用会很麻烦的。你这个string应该是std::string吧,它是一个类,而且是带有引用计数的类,C#里并没有直接对应的东西。把C#设为不安全代码unsafe,调用Heap API等都无助于解决上面的问题。你可以直接用void Convert(LPSTR* str, int length),其实这样反而比较容易一些。
我实际的情况更加复杂void Convert(LPSTR* str, int length) 这样只能获得一个字符串 不能获得一堆字符串偶刚进公司实习就做这么麻烦的事 郁闷啊
先做个DLl把C++中的string 类型转换成 LPCSTR 作为传递参数
然后C#中就有了相应的类型 System.String
相对应的类型参考这个http://dev.csdn.net/develop/article/28/28164.shtm非常感谢大家的回答啊 结贴去