Struct to Byte

G

Guest

Hello,

this my problem:
I have a struc, but i would like to know how can i do to convert it in byte.

This is my code:

[DllImport("winspool.drv", EntryPoint = "AddMonitor")]
public static extern int AddMonitorA(string pName, int Level, ref byte
pMonitors);

[StructLayout(LayoutKind.Sequential)]
public struct MONITOR_INFO_2
{
public string pName;
public string pEnvironment;
public string pDLLName;
}

AddMonitorA(null, 2,ref ???);

Thank you,

Best regards,

Wavemill
 
N

Nicholas Paldino [.NET/C# MVP]

wavemill,

Two things. The first is that I would not specifically point to the
AddMonitorA function. The declaration I would use is this:

[DllImmport("winspool.drv", CharSet=CharSet.Auto)]
public static extern bool AddMonitor(string pName, int Level, ref
MONITOR_INFO_2 pMonitors);

The P/Invoke layer will call the right function
(AddMonitorA/AddMonitorW) based on the platform the code is being run on.
Additionally, you can declare the last parameter as the type you want to
pass. Since the parameter is just a pointer, you can pass any kind you
want. When you use ref, the marshaler considers it a pointer to a structure
and it marshals it appropriately.

Hope this helps.
 

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