Import Structure from unmanaged DLL (not function!)

V

vk000

Hi,

There is a constant structure with predefined fields values located in WinScard.dll. I am trying to get access to this structure
values in my C# code. Is there any way I can do that?

Basically I just need to obtain a memory reference like IntPtr to location of the structure in DLL, since I know the structure
layout.

I know that [DllImport()...] attribute works great for functions but looks like there is no instruction/attribute to associate
IntPtr and structure from an unmanaged DLL.

In my case I am trying to get access to
g_rgSCardT0Pci,
g_rgSCardT1Pci,
g_rgSCardRawPci
structures from WinScard.dll

Please help!

--
Regards,

Victor.
|||||||||||||||||||||||||||||||||||||||||||
"Be nice to the people you meet on the way up, because you'll be meeting the same people on the way down."
 
P

Philip Coveney

Victor,

Try this:

[StructLayout(LayoutKing.Explicit, Size=#bytes)]
internal class SCardInfo (sic)
{
[FieldOffset(0)] public int g_rgSCardT0Pci;
[FieldOffset(4)] public int g_rgSCardT1Pci;
[FieldOffset(8)] public int g_rgSCardRawPci;
}

Basically, you tell .NET exactly how to marshal the structure. Then use
DllImport as you probably already are, roughly:

[DllImport("foo,dll", EntryPoint="MyFunc")]
static extern int GetCardInfo (SCardInfo cardInfo);

(Obviously, I don't know the names/signatures for the functions in your
DLL, but you get the idea.)

Finally, call the sucker:

SCardInfo temp = new SCardInfo();
int result = GetCardInfo(temp);

Good luck,

PC
 

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