Accessing values of an unmanaged C++ pointer to pointer in C#
I want to use c++ code in my C# scripts for rendering some objects in unity 3d.
I have compiled the C++ script as a dll (lammps.dll) and have written a wrapper C# script to invoke a few functions from C++ script.
I am able to access void* and int* pointer successfully by doing the following:
Example:
C++ prototype:
void *lammps_extract_global(void *ptr, char *name);
Equivalent C# prototype (in CSharpLibrary.cs file):
DllImport("lammps.dll", EntryPoint = "lammps_extract_gloabl", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall);
public static extern IntPtr lammps_extract_global(IntPtr ptr, [MarshalAs(UnmamanegType.LPStr)]String name);
Then I compile the C# file as a file which produces CSharpLibrary.dll
In another C# script I am using CSharpLibrary wrapper to access C++ functions as:
1. First define lmp_ptr as,
var lmp_ptr = Marshal.AllocHGlobal(Marshal.Sizeof(typeof(System.IntPtr)));
2. Get lmp_ptr from C++ and dereference it as,
var deref1 = (System.IntPtr)Marshal.PtrToStructure(lmp_ptr, typeof(System.IntPtr))
3. Then use deref1 to get natmPtr as,
System.IntPtr natmPtr = CSharpLibrary.CSharpLibrary.lammps_extract_global(deref1, "natoms");
4. Convert natmPtr to an integer as,
int nAtom = (int)Marshal.PtrToStructure(natmPtr, typeof(int))
Step 4 gives me appropriate value of nAtom.
I want to do the same thing as done for nAtom to another float pointer to pointer matrix storing positions.
I did this to get the float pointer to pointer reference in C#
Considering lammps_extract_atom has been defined similar to the function explain above, we have
System.IntPtr posPtr = CSharpLibrary.CSharpLibrary.lammps_extract_atom(deref1, "x")
Here posPtr is a float** value being returned from C++
Similar statement in C++ would have looked like
float** posPtr = (double**)(lammps_extract_atom)(void *lmp_ptr, char *name)
c# c++ unity3d marshalling unmanaged
|
show 2 more comments
I want to use c++ code in my C# scripts for rendering some objects in unity 3d.
I have compiled the C++ script as a dll (lammps.dll) and have written a wrapper C# script to invoke a few functions from C++ script.
I am able to access void* and int* pointer successfully by doing the following:
Example:
C++ prototype:
void *lammps_extract_global(void *ptr, char *name);
Equivalent C# prototype (in CSharpLibrary.cs file):
DllImport("lammps.dll", EntryPoint = "lammps_extract_gloabl", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall);
public static extern IntPtr lammps_extract_global(IntPtr ptr, [MarshalAs(UnmamanegType.LPStr)]String name);
Then I compile the C# file as a file which produces CSharpLibrary.dll
In another C# script I am using CSharpLibrary wrapper to access C++ functions as:
1. First define lmp_ptr as,
var lmp_ptr = Marshal.AllocHGlobal(Marshal.Sizeof(typeof(System.IntPtr)));
2. Get lmp_ptr from C++ and dereference it as,
var deref1 = (System.IntPtr)Marshal.PtrToStructure(lmp_ptr, typeof(System.IntPtr))
3. Then use deref1 to get natmPtr as,
System.IntPtr natmPtr = CSharpLibrary.CSharpLibrary.lammps_extract_global(deref1, "natoms");
4. Convert natmPtr to an integer as,
int nAtom = (int)Marshal.PtrToStructure(natmPtr, typeof(int))
Step 4 gives me appropriate value of nAtom.
I want to do the same thing as done for nAtom to another float pointer to pointer matrix storing positions.
I did this to get the float pointer to pointer reference in C#
Considering lammps_extract_atom has been defined similar to the function explain above, we have
System.IntPtr posPtr = CSharpLibrary.CSharpLibrary.lammps_extract_atom(deref1, "x")
Here posPtr is a float** value being returned from C++
Similar statement in C++ would have looked like
float** posPtr = (double**)(lammps_extract_atom)(void *lmp_ptr, char *name)
c# c++ unity3d marshalling unmanaged
You want to return float array from C++ to C#?
– Programmer
Nov 26 '18 at 0:40
1
Create the array in C# and update it in C++. See this post. That's the most peformant way to do this. If you want to return Vector3, see this post too. Not a good idea to return non Object pointer from C++. Again, this is due to performance (memory allocation.
– Programmer
Nov 26 '18 at 0:54
1
"I cannot pass the C# array to C++ since the C++ code is quite complex" That doesn't make any sense and yes, you can. I am not sure what you mean by entry point of initialization of the array but you can can pass C# array to C++ from the Awake or Start function to initialize the array if you actually read the post I linked. If you still decide to do it your own way, don't forget to free/delete it on the C++ side and also on the C# side. Goodluck!
– Programmer
Nov 26 '18 at 4:53
1
You can use function Marshal.PtrToStruct docs.microsoft.com/en-us/dotnet/api/…
– Liu Hao
Nov 26 '18 at 9:42
1
@LiuHao Which requiresGCHandle.Alloc
,GCHandle.Free
and freeing the array on the native just like programmer said.
– PassetCronUs
Nov 26 '18 at 21:42
|
show 2 more comments
I want to use c++ code in my C# scripts for rendering some objects in unity 3d.
I have compiled the C++ script as a dll (lammps.dll) and have written a wrapper C# script to invoke a few functions from C++ script.
I am able to access void* and int* pointer successfully by doing the following:
Example:
C++ prototype:
void *lammps_extract_global(void *ptr, char *name);
Equivalent C# prototype (in CSharpLibrary.cs file):
DllImport("lammps.dll", EntryPoint = "lammps_extract_gloabl", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall);
public static extern IntPtr lammps_extract_global(IntPtr ptr, [MarshalAs(UnmamanegType.LPStr)]String name);
Then I compile the C# file as a file which produces CSharpLibrary.dll
In another C# script I am using CSharpLibrary wrapper to access C++ functions as:
1. First define lmp_ptr as,
var lmp_ptr = Marshal.AllocHGlobal(Marshal.Sizeof(typeof(System.IntPtr)));
2. Get lmp_ptr from C++ and dereference it as,
var deref1 = (System.IntPtr)Marshal.PtrToStructure(lmp_ptr, typeof(System.IntPtr))
3. Then use deref1 to get natmPtr as,
System.IntPtr natmPtr = CSharpLibrary.CSharpLibrary.lammps_extract_global(deref1, "natoms");
4. Convert natmPtr to an integer as,
int nAtom = (int)Marshal.PtrToStructure(natmPtr, typeof(int))
Step 4 gives me appropriate value of nAtom.
I want to do the same thing as done for nAtom to another float pointer to pointer matrix storing positions.
I did this to get the float pointer to pointer reference in C#
Considering lammps_extract_atom has been defined similar to the function explain above, we have
System.IntPtr posPtr = CSharpLibrary.CSharpLibrary.lammps_extract_atom(deref1, "x")
Here posPtr is a float** value being returned from C++
Similar statement in C++ would have looked like
float** posPtr = (double**)(lammps_extract_atom)(void *lmp_ptr, char *name)
c# c++ unity3d marshalling unmanaged
I want to use c++ code in my C# scripts for rendering some objects in unity 3d.
I have compiled the C++ script as a dll (lammps.dll) and have written a wrapper C# script to invoke a few functions from C++ script.
I am able to access void* and int* pointer successfully by doing the following:
Example:
C++ prototype:
void *lammps_extract_global(void *ptr, char *name);
Equivalent C# prototype (in CSharpLibrary.cs file):
DllImport("lammps.dll", EntryPoint = "lammps_extract_gloabl", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall);
public static extern IntPtr lammps_extract_global(IntPtr ptr, [MarshalAs(UnmamanegType.LPStr)]String name);
Then I compile the C# file as a file which produces CSharpLibrary.dll
In another C# script I am using CSharpLibrary wrapper to access C++ functions as:
1. First define lmp_ptr as,
var lmp_ptr = Marshal.AllocHGlobal(Marshal.Sizeof(typeof(System.IntPtr)));
2. Get lmp_ptr from C++ and dereference it as,
var deref1 = (System.IntPtr)Marshal.PtrToStructure(lmp_ptr, typeof(System.IntPtr))
3. Then use deref1 to get natmPtr as,
System.IntPtr natmPtr = CSharpLibrary.CSharpLibrary.lammps_extract_global(deref1, "natoms");
4. Convert natmPtr to an integer as,
int nAtom = (int)Marshal.PtrToStructure(natmPtr, typeof(int))
Step 4 gives me appropriate value of nAtom.
I want to do the same thing as done for nAtom to another float pointer to pointer matrix storing positions.
I did this to get the float pointer to pointer reference in C#
Considering lammps_extract_atom has been defined similar to the function explain above, we have
System.IntPtr posPtr = CSharpLibrary.CSharpLibrary.lammps_extract_atom(deref1, "x")
Here posPtr is a float** value being returned from C++
Similar statement in C++ would have looked like
float** posPtr = (double**)(lammps_extract_atom)(void *lmp_ptr, char *name)
c# c++ unity3d marshalling unmanaged
c# c++ unity3d marshalling unmanaged
asked Nov 26 '18 at 0:09
Ankit MishraAnkit Mishra
132113
132113
You want to return float array from C++ to C#?
– Programmer
Nov 26 '18 at 0:40
1
Create the array in C# and update it in C++. See this post. That's the most peformant way to do this. If you want to return Vector3, see this post too. Not a good idea to return non Object pointer from C++. Again, this is due to performance (memory allocation.
– Programmer
Nov 26 '18 at 0:54
1
"I cannot pass the C# array to C++ since the C++ code is quite complex" That doesn't make any sense and yes, you can. I am not sure what you mean by entry point of initialization of the array but you can can pass C# array to C++ from the Awake or Start function to initialize the array if you actually read the post I linked. If you still decide to do it your own way, don't forget to free/delete it on the C++ side and also on the C# side. Goodluck!
– Programmer
Nov 26 '18 at 4:53
1
You can use function Marshal.PtrToStruct docs.microsoft.com/en-us/dotnet/api/…
– Liu Hao
Nov 26 '18 at 9:42
1
@LiuHao Which requiresGCHandle.Alloc
,GCHandle.Free
and freeing the array on the native just like programmer said.
– PassetCronUs
Nov 26 '18 at 21:42
|
show 2 more comments
You want to return float array from C++ to C#?
– Programmer
Nov 26 '18 at 0:40
1
Create the array in C# and update it in C++. See this post. That's the most peformant way to do this. If you want to return Vector3, see this post too. Not a good idea to return non Object pointer from C++. Again, this is due to performance (memory allocation.
– Programmer
Nov 26 '18 at 0:54
1
"I cannot pass the C# array to C++ since the C++ code is quite complex" That doesn't make any sense and yes, you can. I am not sure what you mean by entry point of initialization of the array but you can can pass C# array to C++ from the Awake or Start function to initialize the array if you actually read the post I linked. If you still decide to do it your own way, don't forget to free/delete it on the C++ side and also on the C# side. Goodluck!
– Programmer
Nov 26 '18 at 4:53
1
You can use function Marshal.PtrToStruct docs.microsoft.com/en-us/dotnet/api/…
– Liu Hao
Nov 26 '18 at 9:42
1
@LiuHao Which requiresGCHandle.Alloc
,GCHandle.Free
and freeing the array on the native just like programmer said.
– PassetCronUs
Nov 26 '18 at 21:42
You want to return float array from C++ to C#?
– Programmer
Nov 26 '18 at 0:40
You want to return float array from C++ to C#?
– Programmer
Nov 26 '18 at 0:40
1
1
Create the array in C# and update it in C++. See this post. That's the most peformant way to do this. If you want to return Vector3, see this post too. Not a good idea to return non Object pointer from C++. Again, this is due to performance (memory allocation.
– Programmer
Nov 26 '18 at 0:54
Create the array in C# and update it in C++. See this post. That's the most peformant way to do this. If you want to return Vector3, see this post too. Not a good idea to return non Object pointer from C++. Again, this is due to performance (memory allocation.
– Programmer
Nov 26 '18 at 0:54
1
1
"I cannot pass the C# array to C++ since the C++ code is quite complex" That doesn't make any sense and yes, you can. I am not sure what you mean by entry point of initialization of the array but you can can pass C# array to C++ from the Awake or Start function to initialize the array if you actually read the post I linked. If you still decide to do it your own way, don't forget to free/delete it on the C++ side and also on the C# side. Goodluck!
– Programmer
Nov 26 '18 at 4:53
"I cannot pass the C# array to C++ since the C++ code is quite complex" That doesn't make any sense and yes, you can. I am not sure what you mean by entry point of initialization of the array but you can can pass C# array to C++ from the Awake or Start function to initialize the array if you actually read the post I linked. If you still decide to do it your own way, don't forget to free/delete it on the C++ side and also on the C# side. Goodluck!
– Programmer
Nov 26 '18 at 4:53
1
1
You can use function Marshal.PtrToStruct docs.microsoft.com/en-us/dotnet/api/…
– Liu Hao
Nov 26 '18 at 9:42
You can use function Marshal.PtrToStruct docs.microsoft.com/en-us/dotnet/api/…
– Liu Hao
Nov 26 '18 at 9:42
1
1
@LiuHao Which requires
GCHandle.Alloc
, GCHandle.Free
and freeing the array on the native just like programmer said.– PassetCronUs
Nov 26 '18 at 21:42
@LiuHao Which requires
GCHandle.Alloc
, GCHandle.Free
and freeing the array on the native just like programmer said.– PassetCronUs
Nov 26 '18 at 21:42
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473277%2faccessing-values-of-an-unmanaged-c-pointer-to-pointer-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473277%2faccessing-values-of-an-unmanaged-c-pointer-to-pointer-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You want to return float array from C++ to C#?
– Programmer
Nov 26 '18 at 0:40
1
Create the array in C# and update it in C++. See this post. That's the most peformant way to do this. If you want to return Vector3, see this post too. Not a good idea to return non Object pointer from C++. Again, this is due to performance (memory allocation.
– Programmer
Nov 26 '18 at 0:54
1
"I cannot pass the C# array to C++ since the C++ code is quite complex" That doesn't make any sense and yes, you can. I am not sure what you mean by entry point of initialization of the array but you can can pass C# array to C++ from the Awake or Start function to initialize the array if you actually read the post I linked. If you still decide to do it your own way, don't forget to free/delete it on the C++ side and also on the C# side. Goodluck!
– Programmer
Nov 26 '18 at 4:53
1
You can use function Marshal.PtrToStruct docs.microsoft.com/en-us/dotnet/api/…
– Liu Hao
Nov 26 '18 at 9:42
1
@LiuHao Which requires
GCHandle.Alloc
,GCHandle.Free
and freeing the array on the native just like programmer said.– PassetCronUs
Nov 26 '18 at 21:42