How would I InterOp this structure?

M

Marauderz

Hi guys I'm trying to make a wrapper to the SIMManager's phonebook commands,
has anyone done it? :p anyway I'm running into some problems with defining
the phone book datastructure. The SDK defines it as

typedef struct simphonebookentry_tag {
DWORD cbSize;
DWORD dwParams;
TCHAR lpszAddress[MAX_LENGTH_ADDRESS];
DWORD dwAddressType;
DWORD dwNumPlan;
TCHAR lpszText[MAX_LENGTH_PHONEBOOKENTRYTEXT];
} SIMPHONEBOOKENTRY, *LPSIMPHONEBOOKENTRY;

So would this do the same if I defined it in VB.NEt?
<StructLayout(LayoutKind.Sequential)> _
Public Structure PhoneBookEntryInfo
Public cbSize As Int32
Public dwParams As Int32
<VBFixedString(256)> Public lpszAddress As String
Public dwAddressType As Int32
Public dwNumPlan As Int32
<VBFixedString(256)> Public lpszText As String
End Structure

Evidently it doesn't... cause when I try to send the cbSize parameter by
using a call as follows :-
Dim oRead As New SIMManager.PhoneBookEntryInfo
oRead.cbSize = Marshal.SizeOf(oRead)
But I'm getting a size of 24! which is definetly not what I want!

the call I'm trying to emulate is this :-
HRESULT SimReadPhonebookEntry (
HSIM hSim,
DWORD dwLocation,
DWORD dwIndex,
LPSIMPHONEBOOKENTRY lpPhonebookEntry
);
my dllDeclare is this :-
<DllImport("cellcore.dll")> _
Function SimReadPhoneBookEntry(ByVal hSim As IntPtr, ByVal dwLocation As
Int32, ByVal dwIndex As Int32, ByRef PhoneEntry As PhoneBookEntryInfo) As
Integer

End Function

So how warm am I to getting this right? I know it's wrong cause I'm getting
the usual not supported error... :p

Thanks
 
G

Geoff Schwab [MSFT]

This is a bit tricky due to the embedded string. Take a look at the
following sample. It is usually best to treat the struct as a byte array
and use accessors to access the data - keep in mind a TCHAR is a short array
not a char array. For this example, just modify any functions to take a
byte array instead of the struct.

typedef struct {
WORD wMid;
WORD wPid;
MMVERSION vDriverVersion;
TCHAR szPname[MAXPNAMELEN];
DWORD dwFormats;
WORD wChannels;
WORD wReserved1;
DWORD dwSupport;
} WAVEOUTCAPS;

Converted to...

public class WAVEOUTCAPS
{
const uint WAVEOUTCAPS_SIZE = 84;

public static implicit operator byte[](WAVEOUTCAPS caps)
{
return caps.m_data;
}

private byte[] m_data = null;
public uint Size { get { return (uint)WAVEOUTCAPS_SIZE; } }

public ushort wMid { get { return BitConverter.ToUInt16(m_data, 0); } }
public ushort wPid { get { return BitConverter.ToUInt16(m_data, 2); } }
public uint vDriverVersion { get { return BitConverter.ToUInt32(m_data,
4); } }
public uint dwFormats { get { return BitConverter.ToUInt32(m_data,
72); } }
public ushort wChannels { get { return BitConverter.ToUInt16(m_data,
76); } }
public ushort wReserved1 { get { return BitConverter.ToUInt16(m_data,
78); } }
public uint dwSupport { get { return BitConverter.ToUInt16(m_data,
80); } }

public WAVEOUTCAPS()
{
m_data = new byte[WAVEOUTCAPS_SIZE];
}

public string szPname
{
get
{
char[] bytes = new char[32];
for (int i = 0; i < 32; i++)
{
bytes = (char)BitConverter.ToUInt16(m_data, i * 2 + 8);
}
return new string(bytes);
}
}
}

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
Marauderz said:
Hi guys I'm trying to make a wrapper to the SIMManager's phonebook commands,
has anyone done it? :p anyway I'm running into some problems with defining
the phone book datastructure. The SDK defines it as

typedef struct simphonebookentry_tag {
DWORD cbSize;
DWORD dwParams;
TCHAR lpszAddress[MAX_LENGTH_ADDRESS];
DWORD dwAddressType;
DWORD dwNumPlan;
TCHAR lpszText[MAX_LENGTH_PHONEBOOKENTRYTEXT];
} SIMPHONEBOOKENTRY, *LPSIMPHONEBOOKENTRY;

So would this do the same if I defined it in VB.NEt?
<StructLayout(LayoutKind.Sequential)> _
Public Structure PhoneBookEntryInfo
Public cbSize As Int32
Public dwParams As Int32
<VBFixedString(256)> Public lpszAddress As String
Public dwAddressType As Int32
Public dwNumPlan As Int32
<VBFixedString(256)> Public lpszText As String
End Structure

Evidently it doesn't... cause when I try to send the cbSize parameter by
using a call as follows :-
Dim oRead As New SIMManager.PhoneBookEntryInfo
oRead.cbSize = Marshal.SizeOf(oRead)
But I'm getting a size of 24! which is definetly not what I want!

the call I'm trying to emulate is this :-
HRESULT SimReadPhonebookEntry (
HSIM hSim,
DWORD dwLocation,
DWORD dwIndex,
LPSIMPHONEBOOKENTRY lpPhonebookEntry
);
my dllDeclare is this :-
<DllImport("cellcore.dll")> _
Function SimReadPhoneBookEntry(ByVal hSim As IntPtr, ByVal dwLocation As
Int32, ByVal dwIndex As Int32, ByRef PhoneEntry As PhoneBookEntryInfo) As
Integer

End Function

So how warm am I to getting this right? I know it's wrong cause I'm getting
the usual not supported error... :p

Thanks
 
M

Marauderz

*Ack* OK, I'll digest the code once I get back home. Thanks ;)
Geoff Schwab said:
This is a bit tricky due to the embedded string. Take a look at the
following sample. It is usually best to treat the struct as a byte array
and use accessors to access the data - keep in mind a TCHAR is a short array
not a char array. For this example, just modify any functions to take a
byte array instead of the struct.

typedef struct {
WORD wMid;
WORD wPid;
MMVERSION vDriverVersion;
TCHAR szPname[MAXPNAMELEN];
DWORD dwFormats;
WORD wChannels;
WORD wReserved1;
DWORD dwSupport;
} WAVEOUTCAPS;

Converted to...

public class WAVEOUTCAPS
{
const uint WAVEOUTCAPS_SIZE = 84;

public static implicit operator byte[](WAVEOUTCAPS caps)
{
return caps.m_data;
}

private byte[] m_data = null;
public uint Size { get { return (uint)WAVEOUTCAPS_SIZE; } }

public ushort wMid { get { return BitConverter.ToUInt16(m_data, 0); } }
public ushort wPid { get { return BitConverter.ToUInt16(m_data, 2); } }
public uint vDriverVersion { get { return BitConverter.ToUInt32(m_data,
4); } }
public uint dwFormats { get { return BitConverter.ToUInt32(m_data,
72); } }
public ushort wChannels { get { return BitConverter.ToUInt16(m_data,
76); } }
public ushort wReserved1 { get { return BitConverter.ToUInt16(m_data,
78); } }
public uint dwSupport { get { return BitConverter.ToUInt16(m_data,
80); } }

public WAVEOUTCAPS()
{
m_data = new byte[WAVEOUTCAPS_SIZE];
}

public string szPname
{
get
{
char[] bytes = new char[32];
for (int i = 0; i < 32; i++)
{
bytes = (char)BitConverter.ToUInt16(m_data, i * 2 + 8);
}
return new string(bytes);
}
}
}

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
Marauderz said:
Hi guys I'm trying to make a wrapper to the SIMManager's phonebook commands,
has anyone done it? :p anyway I'm running into some problems with defining
the phone book datastructure. The SDK defines it as

typedef struct simphonebookentry_tag {
DWORD cbSize;
DWORD dwParams;
TCHAR lpszAddress[MAX_LENGTH_ADDRESS];
DWORD dwAddressType;
DWORD dwNumPlan;
TCHAR lpszText[MAX_LENGTH_PHONEBOOKENTRYTEXT];
} SIMPHONEBOOKENTRY, *LPSIMPHONEBOOKENTRY;

So would this do the same if I defined it in VB.NEt?
<StructLayout(LayoutKind.Sequential)> _
Public Structure PhoneBookEntryInfo
Public cbSize As Int32
Public dwParams As Int32
<VBFixedString(256)> Public lpszAddress As String
Public dwAddressType As Int32
Public dwNumPlan As Int32
<VBFixedString(256)> Public lpszText As String
End Structure

Evidently it doesn't... cause when I try to send the cbSize parameter by
using a call as follows :-
Dim oRead As New SIMManager.PhoneBookEntryInfo
oRead.cbSize = Marshal.SizeOf(oRead)
But I'm getting a size of 24! which is definetly not what I want!

the call I'm trying to emulate is this :-
HRESULT SimReadPhonebookEntry (
HSIM hSim,
DWORD dwLocation,
DWORD dwIndex,
LPSIMPHONEBOOKENTRY lpPhonebookEntry
);
my dllDeclare is this :-
<DllImport("cellcore.dll")> _
Function SimReadPhoneBookEntry(ByVal hSim As IntPtr, ByVal
dwLocation
As
Int32, ByVal dwIndex As Int32, ByRef PhoneEntry As PhoneBookEntryInfo) As
Integer

End Function

So how warm am I to getting this right? I know it's wrong cause I'm getting
the usual not supported error... :p

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top