Click here to Skip to main content
15,920,956 members
Home / Discussions / C#
   

C#

 
AnswerRe: Correct marshalling? C# callback from C++ DLL Pin
David Knechtges30-Jun-10 5:07
David Knechtges30-Jun-10 5:07 
Questionstd::map<> in C# Pin
raju_shiva30-Jun-10 1:50
raju_shiva30-Jun-10 1:50 
AnswerRe: std::map in C# Pin
Łukasz Nowakowski30-Jun-10 1:56
Łukasz Nowakowski30-Jun-10 1:56 
AnswerRe: std::map in C# Pin
Pete O'Hanlon30-Jun-10 3:00
mvePete O'Hanlon30-Jun-10 3:00 
AnswerRe: std::map in C# Pin
Laxman Auti30-Jun-10 5:25
Laxman Auti30-Jun-10 5:25 
GeneralRe: std::map in C# Pin
raju_shiva30-Jun-10 20:21
raju_shiva30-Jun-10 20:21 
GeneralRe: std::map in C# Pin
Laxman Auti30-Jun-10 22:46
Laxman Auti30-Jun-10 22:46 
GeneralRe: std::map in C# Pin
raju_shiva30-Jun-10 23:18
raju_shiva30-Jun-10 23:18 
GeneralRe: std::map in C# Pin
Laxman Auti1-Jul-10 1:06
Laxman Auti1-Jul-10 1:06 
GeneralRe: std::map in C# Pin
raju_shiva1-Jul-10 1:46
raju_shiva1-Jul-10 1:46 
AnswerRe: std::map in C# [modified] Pin
Laxman Auti5-Jul-10 8:39
Laxman Auti5-Jul-10 8:39 
AnswerRe: std::map in C# [modified] Pin
Dave Doknjas30-Jun-10 11:30
Dave Doknjas30-Jun-10 11:30 
GeneralRe: std::map in C# Pin
Laxman Auti30-Jun-10 19:44
Laxman Auti30-Jun-10 19:44 
QuestionC#/OpenXML to manipulate Word files. Pin
Wolfram Steinke30-Jun-10 1:11
Wolfram Steinke30-Jun-10 1:11 
Questioncan you help me Pin
sweet_memory30-Jun-10 1:01
sweet_memory30-Jun-10 1:01 
AnswerRe: can you help me Pin
OriginalGriff30-Jun-10 1:14
mveOriginalGriff30-Jun-10 1:14 
AnswerRe: can you help me Pin
sweet_memory30-Jun-10 11:23
sweet_memory30-Jun-10 11:23 
GeneralRe: can you help me Pin
OriginalGriff1-Jul-10 0:53
mveOriginalGriff1-Jul-10 0:53 
AnswerRe: can you help me Pin
I Believe In GOD30-Jun-10 8:52
I Believe In GOD30-Jun-10 8:52 
AnswerRe: can you help me Pin
yu-jian30-Jun-10 18:23
yu-jian30-Jun-10 18:23 
QuestionHow to convert DataTable to MSDATASRC? Pin
vineesh v30-Jun-10 0:29
vineesh v30-Jun-10 0:29 
QuestionHow to invoke native MyFunction(MyStruct* myStructs, unsigned int nMyStructs) from C#? Pin
Chesnokov Yuriy29-Jun-10 22:39
professionalChesnokov Yuriy29-Jun-10 22:39 
AnswerRe: How to invoke native MyFunction(MyStruct* myStructs, unsigned int nMyStructs) from C#? Pin
freakyit1-Jul-10 4:50
freakyit1-Jul-10 4:50 
AnswerRe: How to invoke native MyFunction(MyStruct* myStructs, unsigned int nMyStructs) from C#? Pin
Daniel Grunwald2-Jul-10 0:24
Daniel Grunwald2-Jul-10 0:24 
You can allocate the memory whereever you want. A simple solution might be to allocate it in managed memory as .NET array:
MyStruct[] myStructs = new MyStruct[N];
fixed (MyStruct* pMyStructs = myStructs) {
   // fixed statement ensures the GC doesn't move the memory block while we access it using an unmanaged pointer
   MyFunction(pMyStructs, N);
}

In this case, the GC will take care of freeing the memory.

You can also allocate on the stack (if N is small, for large N you would risk a stack overflow):
MyStruct* pMyStructs = stackalloc MyStruct[N];
MyFunction(pMyStructs, N);

In this case, the memory is freed immediately when leaving the function that allocated it.

If you want to allocate in the unmanaged heap, then use one of the allocation functions in the Marshal class:
IntPtr memory = Marshal.AllocHGlobal(N * sizeof(MyStruct));
try {
	MyStruct* pMyStructs = (MyStruct*)memory.ToPointer();
	MyFunction(pMyStructs, N);
} finally {
	Marshal.FreeHGlobal(memory);
}


You could use Marshal.Copy to copy data from a managed array to an unmanaged pointer - but if you already have a managed array, you can simply use the first approach (fixed statement) to pass that without having to copy anything.
Questionbluetooth transfer issue Pin
Sajjad Izadi29-Jun-10 21:23
Sajjad Izadi29-Jun-10 21:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.