Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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



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
Posted
Updated 27-Mar-15 3:25am
v2

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.
 
Share this answer
 
Comments
RajneeshSaysHello 27-Mar-15 9:41am    
"it implies it has an entry point and can be run in isolation - which it shouldn't"
please explain as why it should not
OriginalGriff 27-Mar-15 9:47am    
Why do you think?
RajneeshSaysHello 27-Mar-15 9:49am    
i dont need to define an entry point that way
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.
 
Share this answer
 
Comments
RajneeshSaysHello 27-Mar-15 9:47am    
can we please ignore naming convention for just a minute i know about, i am using dll name for that purpose for now because i have use for only one function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900