在c#中使用c ++ dll的main函数是否可以

编程入门 行业动态 更新时间:2024-10-25 07:30:16
本文介绍了在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函数是否可以

本文发布于:2023-11-09 22:53:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   dll   main

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!