Hi,
I'm trying to call a win32 function using pinvoke and C# .Net.
The function takes a struct reference as a parameter (lpInfo), but on return
the struct is not filled with data, and I'm suspecting that I have made a
mistake mapping up the struct.
DBSTAT DBenum ( XInt32 hDBase,
LPSTR lpszPath,
XDWORD nLevel,
DBENUMCB Callback,
DWORD UserData,
LPVOID lpInfo);
The C struct is like this (truncated):
typedef struct _seginfo {
CHAR Name [ strSHORT + 1 ];
CHAR Desc [ strLONG + 1 ];
DWORD Created;
WORD DClass;
WORD UType;
Int32 Top;
Int32 Bottom;
WORD wPoints;
FLOAT fHStart;
WORD uHUnit;
FLOAT fVScale;
WORD SmpSize; // for smpFILE = file size
// for smpREGULAR
WORD TixPerSmp;
// for smpRANGE
XDWORD *lpUpdateCount;
// any
Int32 lAliasOffset;
} SGMINFO;
strSHORT = 10;
strLONG = 256;
Can anyone help me with a correct C# mapping? 11 8196
Tor, Can anyone help me with a correct C# mapping?
I'm guessing it should look something like this
struct SGMINFO
{
[MmshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string Name;
[MmshalAs(UnmanagedType.ByValTStr, SizeConst=257
public string Desc;
public uint Created;
public ushort DClass;
public ushort UType;
public int Top;
public int Bottom;
public ushort wPoints;
public float fHStart;
public ushort uHUnit;
public float fVScale;
public ushort SmpSize;
public ushort TixPerSmp;
public IntPtr lpUpdateCount;
public int lAliasOffset;
}
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Hi Mattias,
Thanks for your answer!
The struct is still not filled with data.
I see that a sizeof( SGMINFO) i C is 255 bytes, and Marshal.SizeOf(
typeof( SGMINFO )) in .Net is 344 bytes.
Maybe this is due to the fact that a char in ansi C is 1 byte wheras a char
in C# is 2 bytes?
Or do you think this is not the case?
Regards Tor
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl... Tor,
Can anyone help me with a correct C# mapping?
I'm guessing it should look something like this
struct SGMINFO { [MmshalAs(UnmanagedType.ByValTStr, SizeConst=11)] public string Name; [MmshalAs(UnmanagedType.ByValTStr, SizeConst=257 public string Desc; public uint Created; public ushort DClass; public ushort UType; public int Top; public int Bottom; public ushort wPoints; public float fHStart; public ushort uHUnit; public float fVScale; public ushort SmpSize; public ushort TixPerSmp; public IntPtr lpUpdateCount; public int lAliasOffset; }
Mattias
-- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl... Tor,
Can anyone help me with a correct C# mapping?
I'm guessing it should look something like this
struct SGMINFO { [MmshalAs(UnmanagedType.ByValTStr, SizeConst=11)] public string Name; [MmshalAs(UnmanagedType.ByValTStr, SizeConst=257 public string Desc; public uint Created; public ushort DClass; public ushort UType; public int Top; public int Bottom; public ushort wPoints; public float fHStart; public ushort uHUnit; public float fVScale; public ushort SmpSize; public ushort TixPerSmp; public IntPtr lpUpdateCount; public int lAliasOffset; }
I think it needs a StructLayoutAttribute:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
>I think it needs a StructLayoutAttribute: [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
That's the default for C# structs so it's optional. You don't have to
add it but it doesn't change anything if you do.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
>The struct is still not filled with data.
How are you using it? I see that a sizeof( SGMINFO) i C is 255 bytes,
I find that hard to believe since the Desc field alone is 257 bytes.
and Marshal.SizeOf(typeof( SGMINFO )) in .Net is 344 bytes.
I get 312 or 308 depending on the packing.
Maybe this is due to the fact that a char in ansi C is 1 byte wheras a char in C# is 2 bytes?
The size of a char to the marshaler is whatever you set with the
StructLayout attribute. If you keep the default (CharSet.Ansi) it's
one byte per character.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Oj**************@tk2msftngp13.phx.gbl... The struct is still not filled with data. How are you using it?
public delegate bool EnumCallbackCB( string EnumName, uint UserData );
[ DllImport( "WDBase.dll", CharSet=CharSet.Auto, EntryPoint="DBenumA",
SetLastError=true, ExactSpelling=true )]
public static extern WarriorCommon.DBStat DBEnum( UInt32 DBHandler, string
Path, Int32 Level, EnumCallbackCB Callback, Int32 SizeOfUserData, ref
SGMINFO sgm );
I see that a sizeof( SGMINFO) i C is 255 bytes, I find that hard to believe since the Desc field alone is 257 bytes.
Dont understand it, I checked again and sizeof(SGMINFO) is only 244 in C,
and Marshal.SizeOf(typeof( SGMINFO )) in .Net is 344 bytes. I get 312 or 308 depending on the packing.
Sorry I did't mention that the struct was truncated. The complete listing
goes like this
[ StructLayout( LayoutKind.Sequential )]
public struct SGMINFO{
[[MarshalAs(UnmanagedType.ByValTStr, SizeConst=11)]
public string Name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=257)]
public string Desc;
public uint Created;//DWORD
public uint Modified;//DWORD
public uint Code;//DWORD
public ushort bIsAliased;//WORD
public ushort SType;//WORD
public ushort DType;//WORD
public ushort DClass;//WORD
public ushort UType;//WORD
public int Top;//Int32
public int Bottom;//Int32
public ushort wPoints;//WORD
public float fHStart;//FLOAT
public float fHStep;//FLOAT
public ushort uHUnit;//WORD
public float fVScale;//FLOAT
public float fVMin;//FLOAT
public float fVMax;//FLOAT/
public ushort SmpSize;
public ushort TixPerSmp;//WORD
public ushort SmpPerBlk;//WORD
public ushort IxPerBlk;//WORD
public IntPtr lpUpdateCount;//XDWORD
public int lAliasOffset;//Int32}
Maybe this is due to the fact that a char in ansi C is 1 byte wheras a char in C# is 2 bytes?
The size of a char to the marshaler is whatever you set with the StructLayout attribute. If you keep the default (CharSet.Ansi) it's one byte per character.
Mattias
-- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
Sorry for the changed user name, but I'm at home.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Oj**************@tk2msftngp13.phx.gbl... The struct is still not filled with data.
How are you using it?
I see that a sizeof( SGMINFO) i C is 255 bytes,
I find that hard to believe since the Desc field alone is 257 bytes.
and Marshal.SizeOf(typeof( SGMINFO )) in .Net is 344 bytes.
I get 312 or 308 depending on the packing.
Maybe this is due to the fact that a char in ansi C is 1 byte wheras a char in C# is 2 bytes?
The size of a char to the marshaler is whatever you set with the StructLayout attribute. If you keep the default (CharSet.Ansi) it's one byte per character.
Mattias
-- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
>[ DllImport( "WDBase.dll", CharSet=CharSet.Auto, EntryPoint="DBenumA", SetLastError=true, ExactSpelling=true )] public static extern WarriorCommon.DBStat DBEnum( UInt32 DBHandler, string Path, Int32 Level, EnumCallbackCB Callback, Int32 SizeOfUserData, ref SGMINFO sgm );
You may have to add the attributes [In, Out] to the last parameter to
get changes to marshal back. I find that hard to believe since the Desc field alone is 257 bytes. Dont understand it, I checked again and sizeof(SGMINFO) is only 244 in C,
Then your value for strLONG must be wrong.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:O1*************@TK2MSFTNGP12.phx.gbl... You may have to add the attributes [In, Out] to the last parameter to get changes to marshal back.
Would that be a MarshalAsAttribute declaration?
Regards
Tor
>> You may have to add the attributes [In, Out] to the last parameter to get changes to marshal back.
Would that be a MarshalAsAttribute declaration?
No, the InAttribute and OutAttribute classes.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mattias,
Tanks for very good help!
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl... You may have to add the attributes [In, Out] to the last parameter to get changes to marshal back.
Would that be a MarshalAsAttribute declaration?
No, the InAttribute and OutAttribute classes.
Mattias
-- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven Bethard |
last post by:
In the "empty classes as c structs?" thread, we've been talking in some
detail about my proposed "generic objects" PEP. Based on a number of
suggestions, I'm thinking more and more that instead of...
|
by: pmatos |
last post by:
Hi all,
I have a struct with a:
static map<int, int> rules;
Now, I want it to always be initialized with the pairs (-1, 1), (-2, 2)
and (-3, 3). I don't need to dynamically add or delete...
|
by: ccwork |
last post by:
Hi all,
Here is a sample code segment:
....
typedef PACKED struct
{
union
{
PACKED struct
{
char red:1;
|
by: geri.gan |
last post by:
I have C API just like this:
enum void getinfor(const struct inputinfor *a, const struct outputinfor
** b)
i use p/invok to translate it to
internal static extern void getinfor(ref...
|
by: Giovanni Bajo |
last post by:
Hello,
given the ongoing work on struct (which I thought was a dead module), I was
wondering if it would be possible to add an API to register custom parsing
codes for struct. Whenever I use it...
|
by: Lloyd Dupont |
last post by:
I have an object which is just a thin wrapper over an other object and might
be created in big quantities.
something like that:
// ===== pseudo-code cample =======
class TheObject
{
int data;...
|
by: Vikas |
last post by:
Hi
I have been browsing C struct to C# mapping emails on the newsgroups,
but I haven't been able to find a solution to my problem.
My C structure looks like this:
struct myCstruct {
char *...
|
by: barcaroller |
last post by:
I have a multi-field struct and a memory block (created using malloc) that
contains structured data. If I map the struct to the memory block, am I
guaranteed that the struct fields will be filled...
|
by: Steven Woody |
last post by:
In our project, one used the following struct
#pragma pack(1)
struct Foo {
uint8_t b1;
uint8_t b2;
uint8_t b3;
std::vector<uint8_t buf;
...
};
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |