Click here to Skip to main content
15,896,912 members
Home / Discussions / C#
   

C#

 
QuestionBinding Datagrid to a bidimensional array Pin
hellamasta19-Jan-06 3:21
hellamasta19-Jan-06 3:21 
Questionfor RealCondor Pin
Sasuko19-Jan-06 3:13
Sasuko19-Jan-06 3:13 
AnswerRe: for RealCondor Pin
Colin Angus Mackay19-Jan-06 4:16
Colin Angus Mackay19-Jan-06 4:16 
QuestionNetwork problem Pin
snouto19-Jan-06 3:10
snouto19-Jan-06 3:10 
AnswerRe: Network problem Pin
c#guy381119-Jan-06 6:27
c#guy381119-Jan-06 6:27 
AnswerRe: Network problem Pin
mcljava19-Jan-06 10:35
mcljava19-Jan-06 10:35 
GeneralRe: Network problem Pin
snouto19-Jan-06 10:48
snouto19-Jan-06 10:48 
QuestionMarshal an array of WinAPI structs Pin
Sajit Jacob19-Jan-06 1:41
Sajit Jacob19-Jan-06 1:41 
This does appear to work, however is this really how you're supposed to do it:

I'm calling the WinSpool API method EnumPrinterDrivers:

[DllImport("winspool.drv", CharSet=CharSet.Ansi, SetLastError=true)]
public static extern bool EnumPrinterDrivers
(
[MarshalAs(UnmanagedType.LPWStr)] string pName,
[MarshalAs(UnmanagedType.LPWStr)] string pEnvironment,
uint Level,
IntPtr pBuff,
uint cbBuf,
out uint pcbNeeded,
out uint pcReturned
);

In C speak The pBuff would be a pointer to an array of DRIVER_INFO_1 structs. I have declared these as:

[StructLayout(LayoutKind.Sequential)]
public struct DRIVER_INFO_1
{
[MarshalAs(UnmanagedType.LPStr)] public string sName;
}


Heres my method, it's the pointer arithmatic that I'm curious about, seems to be very clumsy:

public static unsafe string[] ListPrinterDrivers()
{
string[] arrDrivers = new string[]{};
try
{
uint nNeeded = 0;
uint nCount = 0;
IntPtr ptrData = IntPtr.Zero;
//how much do I need
EnumPrinterDrivers(null, null, 1, ptrData, 0, out nNeeded, out nCount );
if ( nNeeded > 0 )
{
//alloc space for array
ptrData = Marshal.AllocHGlobal((int)nNeeded);

bool bOK = EnumPrinterDrivers(null, null, 1, ptrData, nNeeded, out nNeeded, out nCount );
if ( bOK && nCount > 0 )
{
arrDrivers = new string[nCount];
IntPtr ptrArrStructs = ptrData;
for ( int idx=0; idx< nCount; idx++ )
{
DRIVER_INFO_1 dinfo = (DRIVER_INFO_1)Marshal.PtrToStructure(ptrArrStructs, typeof(DRIVER_INFO_1));

System.Diagnostics.Debug.WriteLine("Got Printer driver: " + dinfo.sName );
arrDrivers[idx] = dinfo.sName;

//
//OK so now I increment the pointer. Is there a better way
//
int nSize= Marshal.SizeOf(dinfo);
byte* ptrStruct = (byte*)ptrArrStructs.ToPointer();
ptrStruct += nSize;
ptrArrStructs = new IntPtr((void*)ptrStruct);

}
}
Marshal.FreeHGlobal(ptrData);
}
}
catch ( Exception exp )
{
System.Diagnostics.Debug.WriteLine(exp.ToString());
}

return arrDrivers;
}
QuestionDataGrid Header sorting Arrow Pin
eliasvipin19-Jan-06 1:16
eliasvipin19-Jan-06 1:16 
QuestionApplication hanging while using AxWebBrowser Pin
JayaDurai19-Jan-06 0:46
JayaDurai19-Jan-06 0:46 
QuestionRichTextBox Pin
krieg3819-Jan-06 0:21
krieg3819-Jan-06 0:21 
QuestionOutlook object model Guard Pin
Werner Reyneke19-Jan-06 0:12
Werner Reyneke19-Jan-06 0:12 
AnswerRe: Outlook object model Guard Pin
c#guy381119-Jan-06 6:31
c#guy381119-Jan-06 6:31 
QuestionSUM(Excel.range) Pin
Frank23119-Jan-06 0:07
Frank23119-Jan-06 0:07 
QuestionSerialize a folder Pin
sachu_vidya18-Jan-06 23:48
sachu_vidya18-Jan-06 23:48 
AnswerRe: Serialize a folder Pin
malharone19-Jan-06 6:33
malharone19-Jan-06 6:33 
QuestionRetrieving the previous window size after a Form has been maximized Pin
jjansen18-Jan-06 23:43
jjansen18-Jan-06 23:43 
AnswerRe: Retrieving the previous window size after a Form has been maximized Pin
eliasvipin19-Jan-06 2:09
eliasvipin19-Jan-06 2:09 
GeneralRe: Retrieving the previous window size after a Form has been maximized Pin
jjansen19-Jan-06 4:04
jjansen19-Jan-06 4:04 
QuestionAutomatic comments generator Pin
TheDen18-Jan-06 23:30
TheDen18-Jan-06 23:30 
AnswerRe: Automatic comments generator Pin
tarasn19-Jan-06 0:08
tarasn19-Jan-06 0:08 
QuestionAccess Multiple object using remoting Pin
sohne18-Jan-06 23:03
sohne18-Jan-06 23:03 
AnswerRe: Access Multiple object using remoting Pin
LongRange.Shooter19-Jan-06 1:05
LongRange.Shooter19-Jan-06 1:05 
GeneralRe: Access Multiple object using remoting Pin
sohne19-Jan-06 1:40
sohne19-Jan-06 1:40 
QuestionConverting a string to a double Pin
hellamasta18-Jan-06 22:57
hellamasta18-Jan-06 22:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.