Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
GeneralDatabase Pin
ASGill7-Feb-04 17:16
ASGill7-Feb-04 17:16 
GeneralRe: Database Pin
Heath Stewart7-Feb-04 18:50
protectorHeath Stewart7-Feb-04 18:50 
GeneralMore Thread Troubles; not sure if it's deadlock Pin
Peter Mills7-Feb-04 14:41
Peter Mills7-Feb-04 14:41 
GeneralRe: More Thread Troubles; not sure if it's deadlock Pin
Heath Stewart7-Feb-04 18:58
protectorHeath Stewart7-Feb-04 18:58 
GeneralWell it's got me stumped Pin
Rob Manderson7-Feb-04 14:08
protectorRob Manderson7-Feb-04 14:08 
GeneralRe: Well it's got me stumped Pin
Rob Manderson7-Feb-04 16:25
protectorRob Manderson7-Feb-04 16:25 
GeneralRe: Well it's got me stumped Pin
Heath Stewart7-Feb-04 19:08
protectorHeath Stewart7-Feb-04 19:08 
GeneralRe: Well it's got me stumped Pin
Rob Manderson7-Feb-04 22:25
protectorRob Manderson7-Feb-04 22:25 
You're absolutely right. I iterated my way to a working solution using the unsafe keyword and then, following your advice here, removed it and it still compiles and works.

I kinda feel much like I did 15 or so years ago when I was learning how many *'s to bung in front of a pointer to dereference it Smile | :) Heh I'm still in the early stages of learning C# Smile | :)

My final solution looks like this:

C#
[DllImport("Mpr.dll", EntryPoint="WNetEnumResourceA", 
           CallingConvention=CallingConvention.Winapi)]
<p>
private static extern ErrorCodes WNetEnumResource(
           IntPtr hEnum, ref uint lpcCount, IntPtr buffer, 
           ref uint lpBufferSize);

I guess I have a little way to go in unlearning the pointer way. Nonetheless, I still had problems with trying to handle the IntPtr buffer parameter. I could see that I was getting back the correct values but could see no way to convert the buffer into a struct (in the sense of being able to use struct members to refer to the various fields).

I changed the struct to a class (thanks to Inside C# by Tom Archer) and wrote this line...

C#
Marshal.PtrToStructure(buffer, p);

where p is a NETRESOURCE instance and now I can reference each value using p. syntax. You can see that I was thinking in c++ terms and trying to cast a pointer to a different type.

Hmmm this works but I can't see how to call the WNetEnumResource function in such a way as to return more than 1 instance of the NETRESOURCE structure. The Marshal.PrtToStructure method gives me no way to step down the buffer to the second instance, or the third, of the NETRESOURCE structure within the buffer given that I cannot take a sizeof or the address of a managed object. I can see why the compiler forbids me this Smile | :)

On the other hand, I can't see how to do it in c++ either Smile | :) This is a port of some code I wrote in c++ and, single stepping through the c++ code, I see that each time (whatever value I set as the count of entries to be returned) it returns 1 entry. This makes sense actually - the MSDN entry for the WNetEnumResource API says that the buffer must be large enough to contain not only the NETRESOURCE structure itself but also the strings it points to (which is why I allocated a very large buffer). I'm guessing that the API fills in the structure values and copies the strings immediately afterward - and if it does that then one can't use standard c++ pointer arithmetic to step down the buffer.

Rob Manderson

Paul Watson wrote:What sense would you most dislike loosing?

Ian Darling replied.

Telepathy

Then I'd no longer be able to find out everyones dirty little secrets The Lounge, December 4 2003

GeneralRe: Well it's got me stumped Pin
Heath Stewart8-Feb-04 5:40
protectorHeath Stewart8-Feb-04 5:40 
QuestionHow to make panel scroll when scrollbar is moved? Pin
rul3037-Feb-04 13:45
rul3037-Feb-04 13:45 
AnswerRe: How to make panel scroll when scrollbar is moved? Pin
Heath Stewart7-Feb-04 19:12
protectorHeath Stewart7-Feb-04 19:12 
GeneralRe: How to make panel scroll when scrollbar is moved? Pin
rul3038-Feb-04 7:02
rul3038-Feb-04 7:02 
GeneralRe: How to make panel scroll when scrollbar is moved? Pin
Heath Stewart8-Feb-04 7:07
protectorHeath Stewart8-Feb-04 7:07 
GeneralGUID Conversion Pin
Tristan Rhodes7-Feb-04 8:34
Tristan Rhodes7-Feb-04 8:34 
GeneralRe: GUID Conversion Pin
Heath Stewart7-Feb-04 19:19
protectorHeath Stewart7-Feb-04 19:19 
GeneralRe: GUID Conversion Pin
Tristan Rhodes7-Feb-04 21:08
Tristan Rhodes7-Feb-04 21:08 
GeneralRe: GUID Conversion Pin
leppie7-Feb-04 23:23
leppie7-Feb-04 23:23 
GeneralRe: GUID Conversion Pin
Heath Stewart8-Feb-04 5:32
protectorHeath Stewart8-Feb-04 5:32 
GeneralRe: GUID Conversion Pin
Tristan Rhodes8-Feb-04 8:36
Tristan Rhodes8-Feb-04 8:36 
GeneralRe: GUID Conversion Pin
Nick Parker8-Feb-04 5:45
protectorNick Parker8-Feb-04 5:45 
GeneralCreating an instance of an object ... Pin
Andres Coder7-Feb-04 7:36
Andres Coder7-Feb-04 7:36 
GeneralRe: Creating an instance of an object ... Pin
Nick Parker7-Feb-04 7:56
protectorNick Parker7-Feb-04 7:56 
GeneralRe: Creating an instance of an object ... Pin
Charlie Williams7-Feb-04 8:26
Charlie Williams7-Feb-04 8:26 
GeneralRe: Creating an instance of an object ... Pin
John Kuhn7-Feb-04 8:34
John Kuhn7-Feb-04 8:34 
GeneralRe: Creating an instance of an object ... Pin
Heath Stewart7-Feb-04 19:23
protectorHeath Stewart7-Feb-04 19:23 

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.