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

C#

 
GeneralZooming approach Pin
Meysam Mahfouzi7-Feb-04 18:08
Meysam Mahfouzi7-Feb-04 18:08 
GeneralRe: Zooming approach Pin
Heath Stewart7-Feb-04 19:00
protectorHeath Stewart7-Feb-04 19:00 
GeneralCombo Box problem... Pin
je_gonzalez7-Feb-04 17:27
je_gonzalez7-Feb-04 17:27 
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 
I'm trying to use WNetEnumResource() in C#. I've imported the functions from the system DLL's, allocated buffer memory etc but there's one little step that's got me beat. My code looks approximately like this (extraneous guff removed).
C#
[StructLayout(LayoutKind.Sequential)]
private struct NETRESOURCE 
{ 
    public uint   dwScope; 
    public uint   dwType; 
    public uint   dwDisplayType; 
    public uint   dwUsage; 
    public string lpLocalName; 
    public string lpRemoteName; 
    public string lpComment; 
    public string lpProvider; 
};
<p>
[DllImport("Mpr.dll", EntryPoint="WNetEnumResourceW",
           SetLastError=true, CallingConvention=CallingConvention.Winapi)]
<p>
unsafe private static extern ErrorCodes WNetEnumResource(
           IntPtr hEnum, ref uint lpcCount, byte *buffer, ref uint lpBufferSize);
<p>
private unsafe void EnumerateServers()
{
    uint        bufferSize = 16384;
    byte        *buffer = (byte *) Marshal.AllocHGlobal((int) bufferSize);
    IntPtr      handle = (IntPtr) 0;
    ErrorCodes  result;
    uint        cEntries = 1;
<p>
    result = WNetOpenEnum(ResourceScope.RESOURCE_GLOBALNET, ResourceType.RESOURCETYPE_ANY, 0, 0, out handle);
<p>
    if (result == ErrorCodes.NO_ERROR)
    {
        aData.Add("WNetOpenEnum succeeded");
<p>
        do
        {
            result = WNetEnumResource(handle, ref cEntries, buffer, ref bufferSize);
<p>
            if (result == ErrorCodes.NO_ERROR)
            {
                NETRESOURCE *p = (NETRESOURCE *) buffer;
            }
            else if (result != ErrorCodes.ERROR_NO_MORE_ITEMS)
                break;
        } while (result != ErrorCodes.ERROR_NO_MORE_ITEMS);
    }
    else
        aData.Add("WNetOpenEnum failed");
<p>
     Marshal.FreeHGlobal((IntPtr) buffer);
}

As you can see, I've declared the 3rd parameter (the buffer) in the call to WNetEnumResource() function as a byte pointer and used the Marshal.AllocHGlobal to allocate a buffer. Single stepping through the code shows that the initial WNetOpenEnum() succeeds as does the WNetEnumResource() call.

What I can't seem to do (no matter what I try) is to cast the buffer to a pointer to a NETRESOURCE structure. The compiler informs me that I can't take the address or the size of a managed type.

I've probably missed something blindingly obvious Smile | :) but any hints or help would be appreciated.

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
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 
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 

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.