473,395 Members | 1,639 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,395 software developers and data experts.

C# prototype of C: structure includes void**

I want to access a function implemented in a C-dll with the following
prototype:
getDatabase(CALL_GDS *mStruct)
typedef struct _CALL_GDS {
int ModuleId;
int CPUid;
int argc;
char **argv;//*argv[]
} CALL_GDS;

Now:
1. How would the CALL_GDS (and "char** argv") part look like in the
corresponding
C# prototype?
2. How do I call this C# method ?

Any idea?
C#:

[ StructLayout( LayoutKind.Sequential )]
public struct CALL_GDS
{
public int ModuleId;
public int CPUid;
public int argc;
[MarshalAs(UnmanagedType.ByValArray)]
public IntPtr argv;// pptr //*argv[]
}
Nov 17 '05 #1
2 1489
Yosi,

In C#, you would declare CALL_GDS like this:

[StructLayout(LayoutKind.Sequential)]
public struct CALL_GDS
{
public int ModuleId;
public int CPUid;
public int argc;
public IntPtr argv;
}

You would then declare getDatabase as so:

[DllImport("some.dll")]
public static extern void getDatabase(ref CALL_GDS mStruct);

To get the last array, you will have to handle the marshalling yourself.
You will want to do something like this (after you make the call):

// The structure.
CALL_GDS callGds = new CALL_GDS();

// Make the call.
getDatabase(ref callGds);

// The pointer to the string.
IntPtr stringPointer = IntPtr.Zero;

// The array of items in argc. Preallocate.
string[] argv = new string[callGds.argc];

// Cycle through the number of arguments.
for (int index = 0; index < callGds.argc; ++index)
{
// Get the IntPtr corresponding to the index position.
stringPointer = new IntPtr(callGds.argV.ToInt64() + (index *
IntPtr.Size));

// Marshal the string.
argv[index] = Marshal.PtrToStringAnsi(stringPointer);
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"[Yosi]" <Yo**@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
I want to access a function implemented in a C-dll with the following
prototype:
getDatabase(CALL_GDS *mStruct)
typedef struct _CALL_GDS {
int ModuleId;
int CPUid;
int argc;
char **argv;//*argv[]
} CALL_GDS;

Now:
1. How would the CALL_GDS (and "char** argv") part look like in the
corresponding
C# prototype?
2. How do I call this C# method ?

Any idea?
C#:

[ StructLayout( LayoutKind.Sequential )]
public struct CALL_GDS
{
public int ModuleId;
public int CPUid;
public int argc;
[MarshalAs(UnmanagedType.ByValArray)]
public IntPtr argv;// pptr //*argv[]
}


Nov 17 '05 #2
Great thank you ! It is very helpfull.

the following didn't work will . this return garbage:
stringPointer = new IntPtr(callGds.argV.ToInt64() +...
I change it to :
stringPointer = new IntPtr(Marshal.ReadIntPtr(callGds.argV).ToInt64() +...
now I get the string .
Thanks again, you help me so much I was trying to solve that for more than 3
days.
Yosi.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Yosi,

In C#, you would declare CALL_GDS like this:

[StructLayout(LayoutKind.Sequential)]
public struct CALL_GDS
{
public int ModuleId;
public int CPUid;
public int argc;
public IntPtr argv;
}

You would then declare getDatabase as so:

[DllImport("some.dll")]
public static extern void getDatabase(ref CALL_GDS mStruct);

To get the last array, you will have to handle the marshalling yourself.
You will want to do something like this (after you make the call):

// The structure.
CALL_GDS callGds = new CALL_GDS();

// Make the call.
getDatabase(ref callGds);

// The pointer to the string.
IntPtr stringPointer = IntPtr.Zero;

// The array of items in argc. Preallocate.
string[] argv = new string[callGds.argc];

// Cycle through the number of arguments.
for (int index = 0; index < callGds.argc; ++index)
{
// Get the IntPtr corresponding to the index position.
stringPointer = new IntPtr(callGds.argV.ToInt64() + (index *
IntPtr.Size));

// Marshal the string.
argv[index] = Marshal.PtrToStringAnsi(stringPointer);
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"[Yosi]" <Yo**@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
I want to access a function implemented in a C-dll with the following
prototype:
getDatabase(CALL_GDS *mStruct)
typedef struct _CALL_GDS {
int ModuleId;
int CPUid;
int argc;
char **argv;//*argv[]
} CALL_GDS;

Now:
1. How would the CALL_GDS (and "char** argv") part look like in the
corresponding
C# prototype?
2. How do I call this C# method ?

Any idea?
C#:

[ StructLayout( LayoutKind.Sequential )]
public struct CALL_GDS
{
public int ModuleId;
public int CPUid;
public int argc;
[MarshalAs(UnmanagedType.ByValArray)]
public IntPtr argv;// pptr //*argv[]
}


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass'...
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
6
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
3
by: Steve | last post by:
Hi, I have a keypad that I am scanning to get key states, and am putting those states in a structure. 16 keys, so I have it like keys.status=PRESSSED. to keys.status etc. I can access the...
11
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback...
6
by: Art Cummings | last post by:
Hello all, I'm trying to write a function prototype that includes an array of structures. I'm not sure how to write it. I'm also not sure what the call to a function that uses this prototype...
3
by: June Lee | last post by:
Is that for Class/Object function prototype, I must define the function in header file or .cpp file. MyClass::functionA(); MyClass::functionB(); but for C function prototype, I don't have to...
4
by: vshenoy | last post by:
Hi Guys, I was going through gdbm-1.8.3 source (http://ftp.gnu.org/gnu/gdbm/ gdbm-1.8.3.tar.gz) and found this strange thing : all the exposed functions of gdbm work with GDBM_FILE pointer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.