E
Erialc Berts
Forgive me for being a C# newbie.
I have read all of the newsgroup posts, MSDN articles, and other websites on
interop and marshalling to send and receive a C struct as a parameter or
return value in a C function call from C#.
My situation is slightly different, because the C struct I have is global.
I have tried different approaches/techniques (string, String, std::string,
IntPtr, Marshal, class, etc.), but none work. I was given a DLL written in
C. It defines a structure and declares a global instance of it, such as:
typedef struct errInfo
{
long number;
char* description;
} ERRINFOSTRUCT;
PUBLIC ERRINFOSTRUCT ERRINFO = { 0, " " };
It also has two global functions:
__declspec(dllexport) char * GetErrDesc();
__declspec(dllexport) void SetErr(long, char *);
I need to set the members of ERRINFO directly from C# (not using SetErr),
because :
- I am writing a unit test for GetErrDesc
- The two functions are in two different files ("units")
- We must keep our tests isolated (they cannot depend on other tests or
functions)
In all of my failed attempts to set ERRINFO.description, all of my calls to
GetErrDesc returned a space (" "). So, it was obvious that my code did not
actually set ERRINFO. As a test, I called SetErr(1, "Test"), and GetErrDesc
then returned a garbled string, so I know SetErr actually set ERRINFO.
I declared GetErrDesc using the [DllImport("C.dll")] and wish there was a
similar attribute I could use for declaring ERRINFO, but all I could find
was to create my own ERRINFO structure and marshal a copy of it. However, I
do not understand how this concept should even work, since nothing is tying
the managed C# ERRINFO memory to the unmanaged C ERRINFO memory, because there
is no passing of the struct in a function call.
I need to know: 1) how to set the members of C's global ERRINFO from C#; and
2) why I am getting a garbled string returned. My only recourse is to use
C++.NET, and I would rather stick with C#. I am also being forced to use
VS.NET 2002 right now.
Is there someone out there willing to help me with this, please?
(I have spent two 15-hour days on this and am at a stopping point with C#.)
I have read all of the newsgroup posts, MSDN articles, and other websites on
interop and marshalling to send and receive a C struct as a parameter or
return value in a C function call from C#.
My situation is slightly different, because the C struct I have is global.
I have tried different approaches/techniques (string, String, std::string,
IntPtr, Marshal, class, etc.), but none work. I was given a DLL written in
C. It defines a structure and declares a global instance of it, such as:
typedef struct errInfo
{
long number;
char* description;
} ERRINFOSTRUCT;
PUBLIC ERRINFOSTRUCT ERRINFO = { 0, " " };
It also has two global functions:
__declspec(dllexport) char * GetErrDesc();
__declspec(dllexport) void SetErr(long, char *);
I need to set the members of ERRINFO directly from C# (not using SetErr),
because :
- I am writing a unit test for GetErrDesc
- The two functions are in two different files ("units")
- We must keep our tests isolated (they cannot depend on other tests or
functions)
In all of my failed attempts to set ERRINFO.description, all of my calls to
GetErrDesc returned a space (" "). So, it was obvious that my code did not
actually set ERRINFO. As a test, I called SetErr(1, "Test"), and GetErrDesc
then returned a garbled string, so I know SetErr actually set ERRINFO.
I declared GetErrDesc using the [DllImport("C.dll")] and wish there was a
similar attribute I could use for declaring ERRINFO, but all I could find
was to create my own ERRINFO structure and marshal a copy of it. However, I
do not understand how this concept should even work, since nothing is tying
the managed C# ERRINFO memory to the unmanaged C ERRINFO memory, because there
is no passing of the struct in a function call.
I need to know: 1) how to set the members of C's global ERRINFO from C#; and
2) why I am getting a garbled string returned. My only recourse is to use
C++.NET, and I would rather stick with C#. I am also being forced to use
VS.NET 2002 right now.
Is there someone out there willing to help me with this, please?
(I have spent two 15-hour days on this and am at a stopping point with C#.)