Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


Before Posting...

Please check out the Community Guidelines in the Announcements and Administration Category.

problems using CreateFile -- followup

OSR_Community_UserOSR_Community_User Member Posts: 110,217
Hi everybody!

I've read the answers here, but according to them, I guess
I did not explain myself clearly...

I have an NDIS driver, which I start using StartService(), and
connect to it from my application using DefineDosDevice() and
then CreateFile().

It worked fine till now, but now - I have been asked to support
"multiple clients", i.e.: multiple threads/processes can issue
a CreateFile() and connect to the driver.

The IoCreateDevice in the DriverEntry is:
IoCreateDevice(DriverObject,
sizeof(DEVICE_EXTENSION),
&UnicodeDeviceName,
FILE_DEVICE_TRANSPORT,
0, FALSE, &DeviceObject);

(as you can see - non exclusive).


I'm trying to run the SAME CODE that works now twice.
In both - the same lines are being called, the same symbolic
link name is used etc.

The 1st process succeeds in the CreateFile(), and the other does not.

Now, I tried to run them both with debuggers - I set a breakpoint
before the CreateFile() in both debuggers, and ran it.

When they both reached the CreateFile() line,
I stepped over it (f-10 in VC++), and then stepped over in the over
debugger - and it was successful in both cases!

BUT.. When I put a breakpoint before the function the does
the CreateFile(), and steps over one, and then steps over
in the other debugger - it fails!

Another interesting thing, is that although the Symbolic Link name
is in the system (I checked with WinObj),
I must issue a DefineDosDevice() (that defines the already defined
symbolic link), and only then the CreateFile succeeds... otherwise -
it fails.

Please note that I tried all of the 4 combinations:
a. DefineDosDevice() in both processes
b. DefineDosDevice() in the 1st only
c. DefineDosDevice() in the 2nd only
d. DefineDosDevice() in no process

options c, d failed because the 1st process did not
succeed in the CreateFile()

options a,b - only the 1st process succeeded...


I'm desperate... I've been working on this stuff for a few
days - and nothing!!!!



following is the function that does the CreateFile...
(You may recognize it as the Packet.sys sample from the NT-DDK... ;-)
)

Any ideas?

PVOID
PacketOpenAdapter(LPTSTR AdapterName)
{
LPADAPTER lpAdapter;
BOOLEAN Result;
c_debug (DEBUG_INFO, "Packet32: PacketOpenAdapter started");

lpAdapter=(LPADAPTER)GlobalAllocPtr(
GMEM_MOVEABLE | GMEM_ZEROINIT,
sizeof(ADAPTER)
);


if (lpAdapter==NULL) {
c_error (0, "Packet32: PacketOpenAdapter GlobalAlloc Failed");
return NULL;
}
wsprintf(
lpAdapter->SymbolicLink,
TEXT("\\\\.\\%s%s"),
DOSNAMEPREFIX,
&AdapterName[8]
);

Result=DefineDosDevice(
DDD_RAW_TARGET_PATH,
&lpAdapter->SymbolicLink[4],
AdapterName);


if (Result) {
lpAdapter->hFile=CreateFile(lpAdapter->SymbolicLink,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_FLAG_OVERLAPPED,
0);

if (lpAdapter->hFile != INVALID_HANDLE_VALUE) {
return lpAdapter;
}
else {
int x=GetLastError();
c_error (x, "Packet32: PacketOpenAdapter Could not open adapter");
}
}

GlobalFreePtr(lpAdapter);
return NULL;
}


Note: c_debug and c_error are my macros for debugging.. u may ignore
them...



thanks in advance,

- Barak



> -----Original Message-----
> From: Barak Mandelovich [mailto:[email protected]]
> Sent: Tuesday, March 21, 2000 4:44 AM
> To: NT Developers Interest List
> Subject: [ntdev] problems using CreateFile
>
> Hi everybody, and sorry for troubling you with
> newbies questions, but I have a problem with CreateFile
> that I just can't figure out:
>
> I have a driver, to which I want to connect from my application,
> using CreateFile(), and I have 2 processes that try to
> attach to the driver.
>
> The first succeeds, and the second fails with error code #2
> (ERROR_FILE_NOT_FOUND).
>
> The CreateFile line is this:
> CreateFile(SymbolicLinkName,
> GENERIC_WRITE | GENERIC_READ,
> 0,
> NULL,
> CREATE_ALWAYS,
> FILE_FLAG_OVERLAPPED,0);
>

----------------------------------------------------
Barak Mandelovich Mercury Interactive ltd.
[email protected] 19 Shabazi st.
Tel: +972 3 539 9286 Yehud, 56100
Fax: +972 3 533 1617 Israel
----------------------------------------------------

Comments

  • Why do you do a DefineDosDevice?

    Wouldn't it be better to do a IoCreateSymbolicLink in the driver after
    creating your device?

    something like this

    IoCreateDevice(DriverObject,
    sizeof(DEVICE_EXTENSION),
    &UnicodeDeviceName,
    FILE_DEVICE_TRANSPORT,
    0, FALSE, &DeviceObject);

    RtlInitUnicodeString(&SymLinkName,
    L"\\??\\MyDeviceName");

    IoCreateSymbolicLink(&SymLinkName,
    &UnicodeDeviceName);

    then in your user mode code do a:

    HANDLE PacketOpenAdapter(PCTSTR AdapterName)
    {
    TCHAR strName[MAX_PATH];

    _stprintf(strName,
    _T("\\\\.\\MyDeviceName"));

    return CreateFile(strName,
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL,
    OPEN_ALWAYS,
    0,
    NULL);
    }

    This is the way I've always done it and never had a problem. If this
    doesn't work you would also want to check the Create entry point of your
    driver to make sure it isn't interferring with the second open.

    -----Original Message-----
    From: Barak Mandelovich [mailto:[email protected]]
    Sent: Thursday, March 23, 2000 5:26 AM
    To: NT Developers Interest List
    Subject: [ntdev] problems using CreateFile -- followup


    Hi everybody!

    I've read the answers here, but according to them, I guess
    I did not explain myself clearly...

    I have an NDIS driver, which I start using StartService(), and
    connect to it from my application using DefineDosDevice() and
    then CreateFile().

    It worked fine till now, but now - I have been asked to support
    "multiple clients", i.e.: multiple threads/processes can issue
    a CreateFile() and connect to the driver.

    The IoCreateDevice in the DriverEntry is:
    IoCreateDevice(DriverObject,
    sizeof(DEVICE_EXTENSION),
    &UnicodeDeviceName,
    FILE_DEVICE_TRANSPORT,
    0, FALSE, &DeviceObject);

    (as you can see - non exclusive).


    I'm trying to run the SAME CODE that works now twice.
    In both - the same lines are being called, the same symbolic
    link name is used etc.

    The 1st process succeeds in the CreateFile(), and the other does not.

    Now, I tried to run them both with debuggers - I set a breakpoint
    before the CreateFile() in both debuggers, and ran it.

    When they both reached the CreateFile() line,
    I stepped over it (f-10 in VC++), and then stepped over in the over
    debugger - and it was successful in both cases!

    BUT.. When I put a breakpoint before the function the does
    the CreateFile(), and steps over one, and then steps over
    in the other debugger - it fails!

    Another interesting thing, is that although the Symbolic Link name
    is in the system (I checked with WinObj),
    I must issue a DefineDosDevice() (that defines the already defined
    symbolic link), and only then the CreateFile succeeds... otherwise -
    it fails.

    Please note that I tried all of the 4 combinations:
    a. DefineDosDevice() in both processes
    b. DefineDosDevice() in the 1st only
    c. DefineDosDevice() in the 2nd only
    d. DefineDosDevice() in no process

    options c, d failed because the 1st process did not
    succeed in the CreateFile()

    options a,b - only the 1st process succeeded...


    I'm desperate... I've been working on this stuff for a few
    days - and nothing!!!!



    following is the function that does the CreateFile...
    (You may recognize it as the Packet.sys sample from the NT-DDK... ;-)
    )

    Any ideas?

    PVOID
    PacketOpenAdapter(LPTSTR AdapterName)
    {
    LPADAPTER lpAdapter;
    BOOLEAN Result;
    c_debug (DEBUG_INFO, "Packet32: PacketOpenAdapter started");

    lpAdapter=(LPADAPTER)GlobalAllocPtr(
    GMEM_MOVEABLE | GMEM_ZEROINIT,
    sizeof(ADAPTER)
    );


    if (lpAdapter==NULL) {
    c_error (0, "Packet32: PacketOpenAdapter GlobalAlloc
    Failed");
    return NULL;
    }
    wsprintf(
    lpAdapter->SymbolicLink,
    TEXT("\\\\.\\%s%s"),
    DOSNAMEPREFIX,
    &AdapterName[8]
    );

    Result=DefineDosDevice(
    DDD_RAW_TARGET_PATH,
    &lpAdapter->SymbolicLink[4],
    AdapterName);


    if (Result) {
    lpAdapter->hFile=CreateFile(lpAdapter->SymbolicLink,
    GENERIC_WRITE | GENERIC_READ,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL,
    OPEN_ALWAYS,
    FILE_FLAG_OVERLAPPED,
    0);

    if (lpAdapter->hFile != INVALID_HANDLE_VALUE) {
    return lpAdapter;
    }
    else {
    int x=GetLastError();
    c_error (x, "Packet32: PacketOpenAdapter Could not
    open adapter");
    }
    }

    GlobalFreePtr(lpAdapter);
    return NULL;
    }


    Note: c_debug and c_error are my macros for debugging.. u may ignore
    them...



    thanks in advance,

    - Barak



    > -----Original Message-----
    > From: Barak Mandelovich [mailto:[email protected]]
    > Sent: Tuesday, March 21, 2000 4:44 AM
    > To: NT Developers Interest List
    > Subject: [ntdev] problems using CreateFile
    >
    > Hi everybody, and sorry for troubling you with
    > newbies questions, but I have a problem with CreateFile
    > that I just can't figure out:
    >
    > I have a driver, to which I want to connect from my application,
    > using CreateFile(), and I have 2 processes that try to
    > attach to the driver.
    >
    > The first succeeds, and the second fails with error code #2
    > (ERROR_FILE_NOT_FOUND).
    >
    > The CreateFile line is this:
    > CreateFile(SymbolicLinkName,
    > GENERIC_WRITE | GENERIC_READ,
    > 0,
    > NULL,
    > CREATE_ALWAYS,
    > FILE_FLAG_OVERLAPPED,0);
    >

    ----------------------------------------------------
    Barak Mandelovich Mercury Interactive ltd.
    [email protected] 19 Shabazi st.
    Tel: +972 3 539 9286 Yehud, 56100
    Fax: +972 3 533 1617 Israel
    ----------------------------------------------------
  • Eliyas_YakubEliyas_Yakub Member Posts: 229
    I haven't read the entire thread but I thought I should point out one other
    horrible bug in the old version of packet sample related to
    DefineDosDevices. It creates a symbolic link using DefineDosDevice and
    doesn't remove in the close path. This causes two major problems:

    1) Prevents the driver from unloading.
    2) If you open the device in a loop, after some iteration, the
    DefineDosDevices fails as it runs out of resource to create any more new
    symbolic links.

    You could fix this problem by deleting the link
    (DefineDosDevices(DDD_REMOVE_DEFINITION, )) in the PacketClose or as Nathan
    suggested you can use IoCreateSymbolicLink in the kernel-mode. The updated
    packet sample fixes this problem by creating the link in kernel-mode. Here
    is the pointer

    http://support.microsoft.com/support/kb/articles/Q238/6/52.asp

    Good Luck,
    Eliyas
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Writing WDF Drivers 24 January 2022 Live, Online
Internals & Software Drivers 7 February 2022 Live, Online
Kernel Debugging 21 March 2022 Live, Online
Developing Minifilters 23 May 2022 Live, Online