在c#中使用c ++ dll的main函数是否可以 [英] Is it ok to use main function for c++ dll in c#

查看:134
本文介绍了在c#中使用c ++ dll的main函数是否可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个c ++ dll,其中我返回一个最初作为参数传递给它的修改过的字符串,

i我在c#中调用main函数使用



i have created a c++ dll in which i am returning a modified string originally passed to it as argument,
i am calling main function in my c# using

[DllImport("StringManupulation.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr main(string a);







可以使用这样的主要功能,我没有要求再调用除main之外的其他功能。





你也将如何在c#~main()的每次调用中破坏主函数分配的内存?我想在它们返回值后删除c#中指针分配的memroy



ifstream :: pos_type size;

char * memblock;

char * CodeSend;



//逻辑在这里



删除[ ] memblock;

delete [] CodeSend;

返回CodeSend;

但这在我的情况下不起作用




is it ok to use main function like that, i dont have requriement to call any more functions other than main.


also how would you destory the memory allocated by main function in each call from c# ~main() ? i want to delete the memroy allocated by pointer in c# after they have returned value

ifstream::pos_type size;
char * memblock;
char* CodeSend;

//logic here

delete [] memblock;
delete [] CodeSend;
return CodeSend;
but this would not work in my case

推荐答案

一个DLL文件不应该有一个 main fucntion - 它意味着它有一个入口点,可以单独运行 - 它不应该。



因为你创建了DLL,我会重命名函数ModString或者描述它对参数的作用 - 这就是代码使用它更自我记录。



要释放内存,我有一个发布方法,或者更好的是在已经分配的块中有C#传递该函数可以放置修改后的版本的内存。如果你那样做,那么确保你不会因内存泄漏而更加安全和容易。
A DLL file shouldn't have a main fucntion - it implies it has an entry point and can be run in isolation - which it shouldn't.

Since you created the DLL, I'd rename the function "ModString" or something which describes what it does to the parameter - that way the code using it is more self documenting.

To deallocate the memory, I'd have a "release" method, or better still have the C# pass in an already allocated block of memory in which the function can place the modified version. It's a lot safer and easier than trying to ensure you don't end up with memory leaks if you do it that way.


答案是:不!

1.这是一个保留名称,所以它不会被使用。

2.使用说话名称也是一种对话,所以每个人(甚至在10年内)都知道这个功能是什么。
The answer is: No!
1. It is a "reserved name" so it makes sence not to used.
2. It is also convetion to use "speaking name" so everybody (even in 10 years) know what the function makes.


这篇关于在c#中使用c ++ dll的main函数是否可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆