Configuring VC++ 2005 Express to use with Server 2003 R2 Platform SDK

Started by Vortex, June 10, 2006, 10:09:41 PM

Previous topic - Next topic

Vortex

Hi friends,

Like VC++ Toolkit 2003, you can configure VC++ 2005 Express edition to use with PSDK. You can use also your own C run-time startup library to get small VC++ 2005 executables. This compiler adds some additional security code to your executable to detect buffer overruns. Naturally, this buffer checking increases the size of the final executable. If you would like to have a small executable with the cost of security , you can use some dummy code by adding the following definitions to your source code :

http://66.249.93.104/search?q=cache:ZMuym0ad09kJ:devforums.amd.com/index.php%3Fshowtopic%3D92%26view%3Dgetlastpost+%40__security_check_cookie&hl=tr&gl=tr&ct=clnk&cd=3

int __security_cookie;
void __fastcall __security_check_cookie(void *stackAddress){}


Based on this method, I created a simple module defining the dummy function and the variable :

.386
.model flat,syscall

PUBLIC ___security_cookie

.data?
___security_cookie dd ?

.code
@__security_check_cookie@4 PROC ; emulate fastcall convention
ret
@__security_check_cookie@4 ENDP

END


I added this module to my tiny C run-time startup library reconstructed to use with VC++ 2005 Express & PSDK
With the use of the "dummy security checking function" embedded in the startup library , the source code remains intact.

Those who would like to preserve the functionnality of the buffer overrun detector can link the compiled code against bufferoverflowU.lib or it's variants coming all with Server 2003 R2 PSDK :

http://support.microsoft.com/?id=894573

Depending on your VC++ 2005 Express and PSDK installation, you should edit SetVars.bat to set correctly the paths pointing the tools,include files and libraries :

@SET VSINSTALLDIR=C:\Program Files\VCExpress
@SET VCINSTALLDIR=C:\Program Files\VCExpress\VC
@SET FrameworkDir=C:\WINDOWS\Microsoft.NET\Framework
@SET FrameworkVersion=v2.0.50727
@SET FrameworkSDKDir=C:\Program Files\VCExpress\SDK\v2.0
@if "%VSINSTALLDIR%"=="" goto error_no_VSINSTALLDIR
@if "%VCINSTALLDIR%"=="" goto error_no_VCINSTALLDIR

@echo Setting environment for using Microsoft Visual Studio 2005 x86 tools.

@rem
@rem Root of Visual Studio IDE installed files.
@rem
@set DevEnvDir=C:\Program Files\VCExpress\Common7\IDE

@set PATH=C:\Program Files\VCExpress\Common7\IDE;C:\Program Files\VCExpress\VC\BIN;C:\Program Files\VCExpress\Common7\Tools;C:\Program Files\VCExpress\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\VCExpress\VC\VCPackages;%PATH%
@set INCLUDE=C:\Program Files\VCExpress\VC\INCLUDE;C:\Program Files\PSDK\include;%INCLUDE%
@set LIB=C:\Program Files\VCExpress\VC\LIB;C:\Program Files\PSDK\lib;C:\Program Files\VCExpress\SDK\v2.0\lib;%LIB%
@set LIBPATH=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

@goto end

:error_no_VSINSTALLDIR
@echo ERROR: VSINSTALLDIR variable is not set.
@goto end

:error_no_VCINSTALLDIR
@echo ERROR: VCINSTALLDIR variable is not set.
@goto end

:end


A simple batch file to build a project :

cl /c /Oty2 /Zl /Gs /FoDemo.OBJ Demo.c
link /SUBSYSTEM:WINDOWS /NODEFAULTLIB Demo.obj crt0\crt0.lib kernel32.lib user32.lib gdi32.lib \masm32\lib\msvcrt.lib


I choosed msvcrt.lib from the masm32 library set to avoid linkage against msvcr80.dll

The attachment contains a simple demo with the two methods mentioned above.

[attachment deleted by admin]

GregL

Vortex,

Good stuff.  :U And a great way to avoid the MSVCR80.DLL dependancy (if you don't need the CRT).

I was able to use the VC++ 2005 Express Edition IDE to build a MASM only project by setting it up as a makefile project. It works pretty well. No syntax coloring though. The IDE is a very nice debugger too, in my opinion improved from VC++ 2003.

I also found that if you put an INCLUDELIB c:\masm32\lib\msvcrt.lib in your MASM code you can avoid the MSVCR80.DLL dependancy.

I also found that if you want to use Link.exe 8.00 from the command-line you must run vcvars32.bat, or put 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE' in your path otherwise you get a MSPDB80.DLL error.




Vortex

Thanks Greg. I missed the switch /GS- disabling the buffer overrun checking feature. I modified the batch file to contain this switch and now there is no need anymore to use the dummy function and the variable :

cl /c /Oty2 /Zl /Gs /GS- /FoDemo.OBJ Demo.c
link /SUBSYSTEM:WINDOWS /NODEFAULTLIB Demo.obj crt0\crt0.lib kernel32.lib user32.lib gdi32.lib \masm32\lib\msvcrt.lib

[attachment deleted by admin]

GregL

Vortex,

Ah, that's better.

Right now I am trying to figure out how to force a dynamic link to MSVCRT.DLL instead of MSVCR80.DLL. Maybe using #pragma comment(lib, "msvcrt.lib"), I haven't got it yet.


GregL

Vortex,

I haven't been able to force a dynamic link to MSVCRT.DLL instead of MSVCR80.DLL from a C program in VC++ 2005 Express Edition, I tried various combinations of /NODEFAULTLIB and /DEFAULTLIB. No luck.

I was able to do it in a MASM program.

This explains the new CRT libraries.

I think it has to do with this:
Quote from: MSDNWhen you relink against msvcrt.lib, your final EXE and DLL image will now have a dependency on msvcr80.dll instead of msvcrt.dll.

Later:

I got it to work. If I use

/NODEFAULTLIB
/DEFAULTLIB:MSCVRT.LIB        (from VC++ 6.0)
/GS-

it works.




Vortex

Hi Greg,

Thanks for the info. To say the thruth, I never used the pragma statement to specify the libraries. Your method is a nice one, I will try it :U

GregL


Vortex

This set of #pragma directives inserted to the source code works fine :

#pragma comment(lib,"\\masm32\\lib\\msvcrt.lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"gdi32.lib")
#pragma comment(lib,"crt0\\crt0.lib")


The batch file to build the project :

cl /c /Oty2 /Zl /Gs /GS- /FoDemo.OBJ Demo.c
link /SUBSYSTEM:WINDOWS Demo.obj




[attachment deleted by admin]

GregL

Vortex,

Yes, it does work fine.