473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C#-P/Invoke: How to call function that's return value is a struct

inch
6
Hello all..
I'm new to C# and I've not actually started serious coding. However I know I will need to call some legacy functions from a company DLL. I have tested this using P/Invoke and been quite successull. However, there are a few functions that return a struct as their return value. How do I go about calling these? A simple (fictional) example of the C source is below:
Expand|Select|Wrap|Line Numbers
  1. typedef struct _MxRec {
  2. char Name[32];
  3. double Mx[32];
  4. } MxRec;
  5.  
  6. MxRec GetMatrix(/* some parameters */)
  7. {
  8.  
  9. MxRec TheMatrix;
  10.  
  11. strcpy(TheMatrix.Name,"MyName");
  12.  
  13. TheMatrix.Mx[0]=1.0;
  14. // More values
  15. TheMatrix.Mx[11]=123.45;
  16.  
  17. return(TheMatrix);
  18. }
  19.  
Sep 25 '07 #1
6 7170
Plater
7,872 Expert 4TB
I *think* you can create an identical instance of the struct in your code.
(i.e. re-declare it in C# code)
Then you can use that as the return type.

You may have to fiddle with Marshalling settings, but I am unfamiliar with those.
Sep 25 '07 #2
r035198x
13,262 8TB
Hello all..
I'm new to C# and I've not actually started serious coding. However I know I will need to call some legacy functions from a company DLL. I have tested this using P/Invoke and been quite successull. However, there are a few functions that return a struct as their return value. How do I go about calling these? A simple (fictional) example of the C source is below:
Expand|Select|Wrap|Line Numbers
  1. typedef struct _MxRec {
  2. char Name[32];
  3. double Mx[32];
  4. } MxRec;
  5.  
  6. MxRec GetMatrix(/* some parameters */)
  7. {
  8.  
  9. MxRec TheMatrix;
  10.  
  11. strcpy(TheMatrix.Name,"MyName");
  12.  
  13. TheMatrix.Mx[0]=1.0;
  14. // More values
  15. TheMatrix.Mx[11]=123.45;
  16.  
  17. return(TheMatrix);
  18. }
  19.  
You do realize of course that C# also has the struct datatype?
Sep 25 '07 #3
inch
6
You do realize of course that C# also has the struct datatype?
Yea, I got that. When I tried the code below I get the message:
Method's type signature is not PInvoke compatible.
This is the C# code I used:
Expand|Select|Wrap|Line Numbers
  1. [DllImport("CLibToCallFromCS.dll")]
  2. public static extern MxRecArr ReturnStructArr(int Mode);
  3.  
  4. private void button7_Click(object sender, EventArgs e)
  5. {
  6. int var = 0;
  7. MxRecArr MyRec = new MxRecArr();
  8. MyRec.Initialize();
  9. MyRec = ReturnStructArr(var);
  10. }
  11.  
  12. public struct MxRecArr
  13. {
  14. [MarshalAs(UnmanagedType.ByValArray,SizeConst=12)]
  15. public double[] Mx;
  16. // Need to call this after declaration before it can be used
  17. public void Initialize()
  18. {
  19. Mx = new double[12];
  20. }
  21. }
Using this same structure as a parameter in a PInvoke function call worked fine.
Sep 26 '07 #4
inch
6
Just a small update to this. If I try using a 'simple' struct that only has single variable members it works OK.
However using a struct with an array member fails.
There must be a way to do this, but I can't find it.
Sep 27 '07 #5
Plater
7,872 Expert 4TB
Ahhh, yes, I don't believe structs can have an array.
Something about not being able to assign enough space or something
Sep 27 '07 #6
inch
6
Ahhh, yes, I don't believe structs can have an array.
Something about not being able to assign enough space or something
But passing the same struct as a parameter works OK. It is only trying to set it's value when returned as the result of a call that fails.
Oct 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
33
by: Pietro Cerutti | last post by:
Hi group, assume the following declarations: char *func_1(void); void func_2(char **); I am allowed to do: char *c = func_1();
11
by: Shilpa Sethi | last post by:
e.g. The func returns 11 on this execution? Can anyone elucidate its reason? func() { printf("Hello World"); } int main() { func()
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.