linking against kernel32.lib when calling a MASM proc from a C program

Started by allynm, April 04, 2011, 10:06:58 PM

Previous topic - Next topic

allynm

Hello everyone,

I was refreshing my memory on how to call MASM procs from a C program.  I've tried to find the answer to my question on forum posts.  Several were helpful but they didn't quite get my question nailed down.

If I use the traditional "includelib \masm32\lib\kernel32.lib"  statement then when I try to link myCprog.obj with myMASMfunction.obj I get an error saying that GetEnvironmentStrings can't be found.  If I instead do:  "includelib c:\program files\microsoft sdks\windows\v6.o\lib\kernel32.lib"  all goes well.

Changing the includelib statement is not a big deal (or else simply doing link....   kernel32.lib in the the link step).  Just have to remember to do it.

My question is:  how did this come to be?  Or, am I missing something obvious?  Why is the kernel32 module in MASM different (apparently) from the Windows SDKS version?  Maybe the problem can be solved in the companion .inc file?   Does this sort of problem crop up with other import libraries?

Thanks for any info you can provide.

Regards,
Mark Allyn

clive

If you had included kernel32.inc (for the definitions rather than the linkage) it would have substituted GetEnvironmentStrings with GetEnvironmentStringsA. The kernel32.lib with MASM32 does not contain GetEnvironmentStrings, only GetEnvironmentStringsA and GetEnvironmentStringsW. We'll assume the lib you pulled has all three.
It could be a random act of randomness. Those happen a lot as well.

allynm

Hello Clive,

Thanks for responding.  As a matter of fact, I did include the kernel32.inc definition.  I had read some earlier forums (back in 2006) that suggested that this was the problem with kernel32.lib.  I checked the kernel32.inc  on my drive and it certainly does define GetEnvironmentStrings so that it is equivalent to GetEnvironmentStringsA.  So, I am puzzled.  If you have any more suggestions, I would be happy to try them out.

Regards,
Mark Allyn

clive

This seems to assemble/link without issue. Although if you change drives prior to linking, you'll need to explicitly call out the drive letter to give it a fully qualified path.

; \tools\masm615\ml -coff -Fl -Zi test85.asm -link /SUBSYSTEM:CONSOLE /DEBUG /DEBUGTYPE:BOTH

        .386
        .model flat,stdcall
        option casemap:none

        include \masm32\include\kernel32.inc
        includelib \masm32\lib\kernel32.lib

.code

start:

        invoke GetEnvironmentStrings

        push 0
        call ExitProcess

        end     start
It could be a random act of randomness. Those happen a lot as well.

allynm

Hi Clive

Thanks for hanging in on this.  I copied your code and ran the command line shown below.  No dice.  I get an error saying "unresolved external symbol _GetEnvironmentStrings@0".  I renamed your code "ClivesKernel32.asm"

Command line:

ml  /coff /Zi ClivesKernel32.asm /link /subsystem:console

Regards,
Mark

allynm

Hello again, Clive

I should have mentioned in my response to your last message that when I use the command line:

ml /coff /Zi cliveskernel32.asm /link /subsystem:console kernel32.lib

...the program compiles, links and runs.

Regards again,
Mark

drizz

The truth cannot be learned ... it can only be recognized.

allynm

Good morning Drizz and Clive,

I tried Drizz's suggesstion to replace the kernel32.inc def of GetEnvironmentStrings.  Sorry to report  that this did not fix the linkage problem.  If I link Cliveskernel32.obj using /SUBSYSTEM:CONSOLE and kernel32.lib the Cliveskernel32.obj links and runs.  I'm scratching my head.

Thanks for your suggestions.  I'm willing to carry on if you have further ideas.

Regards,

Mark

dedndave

in your original post, you said you included either the INC or the LIB, if i understood correctly
i am a little confused - lol - could you post the code that is not working ?
as in Clive's example, you want both
        include    \masm32\include\kernel32.inc
        includelib \masm32\lib\kernel32.lib

although, i probably would have put the INCLUDELIB statements in the INC files
for example, the advapi32.inc file would have the includelib for advapi32.lib
it would make no sense to include the inc file if you were not intending to use the lib   :P

allynm

Hi Dedndave, Drizz, and Clive,

In looking back at my work I see I made a really stupid rooky error.  In the source for the asm proc I included \masm32\include\masm32rt.inc.  Pretty dumb since the C program was itself going to load kernel32 and a lot of other stuff too.  When I eliminated the offending .inc from the asm source, everything worked as it should have.

I regret cranking you guys up over such a silly thing.

Regards and thanks,
Mark