Calling a C function from ASM in user-mode driver

I have a user-mode driver (DLL) that I’m compiling with the WDK. The driver
uses an ASM file that’s included in the build process like so:

I386_SOURCES= \
i386\stub.asm

The ASM file is calls a C-function, but for some reason the linker ends up
with an unresolved symbol error.

The C-function I’m trying to call is defined as follows:

extern “C” void GetPtrGetKtProcAddress()
{
}

The ASM file looks as follows:

.586
.MODEL FLAT, C

EXTRN GetPtrGetKtProcAddress:proc

.DATA

.CODE

; Function prototypes
public GetKtProcAddress

GetKtProcAddress proc
pushad
call GetPtrGetKtProcAddress
ret

GetKtProcAddress endp

_text ends
end

The code compiles and links fine in VS2010, so I’m thinking I’m missing
something specific to the WDK somewhere.

The WDK specifies different calling conventions than Visual C’s
defaults. You need to explicitly define the calling convention for the
C routine.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

“Soren Dreijer” wrote in message
news:xxxxx@ntdev:

> I have a user-mode driver (DLL) that I’m compiling with the WDK. The driver
> uses an ASM file that’s included in the build process like so:
>
> I386_SOURCES= <br>> i386\stub.asm
>
> The ASM file is calls a C-function, but for some reason the linker ends up
> with an unresolved symbol error.
>
> The C-function I’m trying to call is defined as follows:
>
> extern “C” void GetPtrGetKtProcAddress()
> {
> }
>
> The ASM file looks as follows:
>
> .586
> .MODEL FLAT, C
>
> EXTRN GetPtrGetKtProcAddress:proc
>
> .DATA
>
> .CODE
>
> ; Function prototypes
> public GetKtProcAddress
>
> GetKtProcAddress proc
> pushad
> call GetPtrGetKtProcAddress
> ret
>
> GetKtProcAddress endp
>
> _text ends
> end
>
> The code compiles and links fine in VS2010, so I’m thinking I’m missing
> something specific to the WDK somewhere.

Well, there we go. That was easy. :slight_smile:

I had already explicitly tried __stdcall but that obviously didn’t work
since the prototype I had in the ASM file was for a __cdecl function.

Cheers!

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:bounce-441913-
xxxxx@lists.osr.com] On Behalf Of Don Burn
Sent: Monday, February 21, 2011 12:49 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Calling a C function from ASM in user-mode driver

The WDK specifies different calling conventions than Visual C’s defaults.
You
need to explicitly define the calling convention for the C routine.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

“Soren Dreijer” wrote in message
> news:xxxxx@ntdev:
>
> > I have a user-mode driver (DLL) that I’m compiling with the WDK. The
> > driver uses an ASM file that’s included in the build process like so:
> >
> > I386_SOURCES= <br>> > i386\stub.asm
> >
> > The ASM file is calls a C-function, but for some reason the linker
> > ends up with an unresolved symbol error.
> >
> > The C-function I’m trying to call is defined as follows:
> >
> > extern “C” void GetPtrGetKtProcAddress() { }
> >
> > The ASM file looks as follows:
> >
> > .586
> > .MODEL FLAT, C
> >
> > EXTRN GetPtrGetKtProcAddress:proc
> >
> > .DATA
> >
> > .CODE
> >
> > ; Function prototypes
> > public GetKtProcAddress
> >
> > GetKtProcAddress proc
> > pushad
> > call GetPtrGetKtProcAddress
> > ret
> >
> > GetKtProcAddress endp
> >
> > _text ends
> > end
> >
> > The code compiles and links fine in VS2010, so I’m thinking I’m
> > missing something specific to the WDK somewhere.
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer

__cdecl: _FuncName
__stdcall: xxxxx@DecimalParamSizeInBytes


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

“Soren Dreijer” wrote in message news:xxxxx@ntdev…
> Well, there we go. That was easy. :slight_smile:
>
> I had already explicitly tried stdcall but that obviously didn’t work
> since the prototype I had in the ASM file was for a
cdecl function.
>
> Cheers!
>
>> -----Original Message-----
>> From: xxxxx@lists.osr.com [mailto:bounce-441913-
>> xxxxx@lists.osr.com] On Behalf Of Don Burn
>> Sent: Monday, February 21, 2011 12:49 PM
>> To: Windows System Software Devs Interest List
>> Subject: Re:[ntdev] Calling a C function from ASM in user-mode driver
>>
>> The WDK specifies different calling conventions than Visual C’s defaults.
> You
>> need to explicitly define the calling convention for the C routine.
>>
>>
>> Don Burn (MVP, Windows DKD)
>> Windows Filesystem and Driver Consulting
>> Website: http://www.windrvr.com
>> Blog: http://msmvps.com/blogs/WinDrvr
>>
>>
>>
>>
>> “Soren Dreijer” wrote in message
>> news:xxxxx@ntdev:
>>
>> > I have a user-mode driver (DLL) that I’m compiling with the WDK. The
>> > driver uses an ASM file that’s included in the build process like so:
>> >
>> > I386_SOURCES= <br>>> > i386\stub.asm
>> >
>> > The ASM file is calls a C-function, but for some reason the linker
>> > ends up with an unresolved symbol error.
>> >
>> > The C-function I’m trying to call is defined as follows:
>> >
>> > extern “C” void GetPtrGetKtProcAddress() { }
>> >
>> > The ASM file looks as follows:
>> >
>> > .586
>> > .MODEL FLAT, C
>> >
>> > EXTRN GetPtrGetKtProcAddress:proc
>> >
>> > .DATA
>> >
>> > .CODE
>> >
>> > ; Function prototypes
>> > public GetKtProcAddress
>> >
>> > GetKtProcAddress proc
>> > pushad
>> > call GetPtrGetKtProcAddress
>> > ret
>> >
>> > GetKtProcAddress endp
>> >
>> > _text ends
>> > end
>> >
>> > The code compiles and links fine in VS2010, so I’m thinking I’m
>> > missing something specific to the WDK somewhere.
>>
>>
>> —
>> NTDEV is sponsored by OSR
>>
>> For our schedule of WDF, WDM, debugging and other seminars visit:
>> http://www.osr.com/seminars
>>
>> To unsubscribe, visit the List Server section of OSR Online at
>> http://www.osronline.com/page.cfm?name=ListServer
>
>

Just add _ before the asm name:

_GetKtProcAddress proc

Also, make sure you handle stack correctly :wink:

Gene

On 22/02/11 07:53, Soren Dreijer wrote:

Well, there we go. That was easy. :slight_smile:

I had already explicitly tried __stdcall but that obviously didn’t work
since the prototype I had in the ASM file was for a __cdecl function.

Cheers!

> -----Original Message-----
> From: xxxxx@lists.osr.com [mailto:bounce-441913-
> xxxxx@lists.osr.com] On Behalf Of Don Burn
> Sent: Monday, February 21, 2011 12:49 PM
> To: Windows System Software Devs Interest List
> Subject: Re:[ntdev] Calling a C function from ASM in user-mode driver
>
> The WDK specifies different calling conventions than Visual C’s defaults.
You
> need to explicitly define the calling convention for the C routine.
>
>
> Don Burn (MVP, Windows DKD)
> Windows Filesystem and Driver Consulting
> Website: http://www.windrvr.com
> Blog: http://msmvps.com/blogs/WinDrvr
>
>
>
>
> “Soren Dreijer” wrote in message
>> news:xxxxx@ntdev:
>>
>>> I have a user-mode driver (DLL) that I’m compiling with the WDK. The
>>> driver uses an ASM file that’s included in the build process like so:
>>>
>>> I386_SOURCES= <br>>>> i386\stub.asm
>>>
>>> The ASM file is calls a C-function, but for some reason the linker
>>> ends up with an unresolved symbol error.
>>>
>>> The C-function I’m trying to call is defined as follows:
>>>
>>> extern “C” void GetPtrGetKtProcAddress() { }
>>>
>>> The ASM file looks as follows:
>>>
>>> .586
>>> .MODEL FLAT, C
>>>
>>> EXTRN GetPtrGetKtProcAddress:proc
>>>
>>> .DATA
>>>
>>> .CODE
>>>
>>> ; Function prototypes
>>> public GetKtProcAddress
>>>
>>> GetKtProcAddress proc
>>> pushad
>>> call GetPtrGetKtProcAddress
>>> ret
>>>
>>> GetKtProcAddress endp
>>>
>>> _text ends
>>> end
>>>
>>> The code compiles and links fine in VS2010, so I’m thinking I’m
>>> missing something specific to the WDK somewhere.
>>
>> —
>> NTDEV is sponsored by OSR
>>
>> For our schedule of WDF, WDM, debugging and other seminars visit:
>> http://www.osr.com/seminars
>>
>> To unsubscribe, visit the List Server section of OSR Online at
>> http://www.osronline.com/page.cfm?name=ListServer
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

That was not the issue though. The issue was that the wrong default calling
convention was used by the compiler for my C function such that it got
mangled (as Maxim showed).

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:bounce-441934-
xxxxx@lists.osr.com] On Behalf Of Gene Soudlenkov
Sent: Monday, February 21, 2011 2:33 PM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] Calling a C function from ASM in user-mode driver

Just add _ before the asm name:

_GetKtProcAddress proc

Also, make sure you handle stack correctly :wink:

Gene

On 22/02/11 07:53, Soren Dreijer wrote:
> Well, there we go. That was easy. :slight_smile:
>
> I had already explicitly tried __stdcall but that obviously didn’t
> work since the prototype I had in the ASM file was for a __cdecl
function.
>
> Cheers!
>
>> -----Original Message-----
>> From: xxxxx@lists.osr.com [mailto:bounce-441913-
>> xxxxx@lists.osr.com] On Behalf Of Don Burn
>> Sent: Monday, February 21, 2011 12:49 PM
>> To: Windows System Software Devs Interest List
>> Subject: Re:[ntdev] Calling a C function from ASM in user-mode driver
>>
>> The WDK specifies different calling conventions than Visual C’s
defaults.
> You
>> need to explicitly define the calling convention for the C routine.
>>
>>
>> Don Burn (MVP, Windows DKD)
>> Windows Filesystem and Driver Consulting
>> Website: http://www.windrvr.com
>> Blog: http://msmvps.com/blogs/WinDrvr
>>
>>
>>
>>
>> “Soren Dreijer” wrote in message
> >> news:xxxxx@ntdev:
> >>
> >>> I have a user-mode driver (DLL) that I’m compiling with the WDK. The
> >>> driver uses an ASM file that’s included in the build process like so:
> >>>
> >>> I386_SOURCES= <br>> >>> i386\stub.asm
> >>>
> >>> The ASM file is calls a C-function, but for some reason the linker
> >>> ends up with an unresolved symbol error.
> >>>
> >>> The C-function I’m trying to call is defined as follows:
> >>>
> >>> extern “C” void GetPtrGetKtProcAddress() { }
> >>>
> >>> The ASM file looks as follows:
> >>>
> >>> .586
> >>> .MODEL FLAT, C
> >>>
> >>> EXTRN GetPtrGetKtProcAddress:proc
> >>>
> >>> .DATA
> >>>
> >>> .CODE
> >>>
> >>> ; Function prototypes
> >>> public GetKtProcAddress
> >>>
> >>> GetKtProcAddress proc
> >>> pushad
> >>> call GetPtrGetKtProcAddress
> >>> ret
> >>>
> >>> GetKtProcAddress endp
> >>>
> >>> _text ends
> >>> end
> >>>
> >>> The code compiles and links fine in VS2010, so I’m thinking I’m
> >>> missing something specific to the WDK somewhere.
> >>
> >> —
> >> NTDEV is sponsored by OSR
> >>
> >> For our schedule of WDF, WDM, debugging and other seminars visit:
> >> http://www.osr.com/seminars
> >>
> >> To unsubscribe, visit the List Server section of OSR Online at
> >> http://www.osronline.com/page.cfm?name=ListServer
> >
> > —
> > NTDEV is sponsored by OSR
> >
> > For our schedule of WDF, WDM, debugging and other seminars visit:
> > http://www.osr.com/seminars
> >
> > To unsubscribe, visit the List Server section of OSR Online at
> > http://www.osronline.com/page.cfm?name=ListServer
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer

extern “C” means you are compiling in C++.

You did not specify the calling convention.

extern “C” void __cdecl GetPtrGetKtProcAddress();

the name is therefore

_GetPtrGetKtProcAddress

and if in a DLL might be

__imp__GetPtrGetKtProcAddress

when in doubt, use some useful utility to examine the file you are linking
to, like the dumpbin utility
joe

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Soren Dreijer
Sent: Monday, February 21, 2011 1:41 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] Calling a C function from ASM in user-mode driver

I have a user-mode driver (DLL) that I’m compiling with the WDK. The driver
uses an ASM file that’s included in the build process like so:

I386_SOURCES= \
i386\stub.asm

The ASM file is calls a C-function, but for some reason the linker ends up
with an unresolved symbol error.

The C-function I’m trying to call is defined as follows:

extern “C” void GetPtrGetKtProcAddress()
{
}

The ASM file looks as follows:

.586
.MODEL FLAT, C

EXTRN GetPtrGetKtProcAddress:proc

.DATA

.CODE

; Function prototypes
public GetKtProcAddress

GetKtProcAddress proc
pushad
call GetPtrGetKtProcAddress
ret

GetKtProcAddress endp

_text ends
end

The code compiles and links fine in VS2010, so I’m thinking I’m missing
something specific to the WDK somewhere.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.