In VC ++, all default libraries are omitted for Win32 API compilation and links.

Source: Internet
Author: User

When we use VC ++ to write a Windows program, we may find that the executable body (. EXE) files are relatively large, so they envy those who use Win32 assembly to write the process order, because their executable files are very small. In fact, the application volume is one aspect, and the deployment environment of the application is another aspect that requires attention. I have a deep understanding of this aspect, I used Visual Studio 2008 to compile a C ++ Win32 program. The local test is normal, but what dynamic library is missing when I deploy it to the client, therefore, Visual C ++ 2008 Redistributable Package (Visual C ++ 2005 Redistributable Package) must be installed, which brings some trouble to software deployment, in addition, for a program with simple functions, installing such a component package may be difficult to understand. We hope that some simple applications can directly call the APIS provided by the system, this reduces the complexity of the deployment Program.

In fact, for VC ++, we can ignore all default libraries to prevent the compiler from introducing unnecessary Dynamic Link Libraries. Of course, you can use the following pre-compiled macros.

# Pragma comment (linker, "/nodefaultlib ")

# Pragma comment (linker, "/nodefaultlib ")

In fact, you also need to change the generated list to "no" in the "connector"> "configuration file", and then select "configuration tool"> "input" and "output" to change the embedded list to "no; in C/C ++, select code generation to change the buffer security check to "no" (/GS-). Otherwise, an error occurs during compilation and set the main entry point of the program. Note that the above configuration is generally in Release, and the generated file is also compiled in Release. Debug may not be used. To prevent compilation in Debug mode, you can use the following macro command:

# Ifdef _ DEBUG
# Error Debug is disabled
# Endif // _ DEBUG

# Ifdef _ DEBUG # error Debug is disabled # endif // _ DEBUG

In addition, you can use the following macro to redefine the function entry to replace attribute configuration.

# Pragma comment (linker, "/ENTRY: wWinMainCRTStartup ")

# Pragma comment (linker, "/ENTRY: wWinMainCRTStartup ")

Because the wide character (Unicode) API call is specified here, we define the entry point as wWinMainCRTStartup, And the ANSI version is recommended as WinMainCRTStartup. In addition, Windows APIs have two versions of API interfaces, ANSI and Unicode are mainly used to be compatible with old systems such as Windows 98. Generally, the program compiled by ansi api is small in size, however, since the Unicode API is basically used by the new NT-based operating system, for these systems, the Unicode API interface is faster than the ANSI interface.

The entry points are defined as follows:

# Include
HINSTANCE g_hInst = NULL;
Int WINAPI SimpleMain (VOID)
{
Return 0;
}
Void winapi wWinMainCRTStartup (VOID)
{
G_hInst = GetModuleHandle (NULL );
ExitProcess (SimpleMain ());
}

# Include HINSTANCE g_hInst = NULL; int WINAPI SimpleMain (VOID) {return 0;} void winapi wWinMainCRTStartup (VOID) {g_hInst = GetModuleHandle (NULL); ExitProcess (SimpleMain ());}

Many friends may also need to obtain the execution command line of the program to obtain the required execution parameters. We can use the GetCommandLine () API to obtain the required execution parameters. Here is a function for separating commands I wrote.

Void winapi AnalyseCommandLine (const LPTSTR lpOriginal, LPTSTR lpParameter)
{
Int I, j, k = 0, n1_len = lstrlen (lpOriginal );
For (I = nw.len; I> 0; I --)
{
If (* (lporiginal + I) = '')
{
For (j = I + 1; J {
* (Lpparameter + k) = * (lporiginal + J );
K ++;
}
Break;
}
}
* (Lpparameter + k) = '/0 ';
}

Void winapi analysecommandline (const lptstr lporiginal, lptstr lpparameter) {int I, j, k = 0, n1_len = lstrlen (lporiginal); for (I = n1_len; I> 0; I --) {If (* (lporiginal + I) = '') {for (j = I + 1; j

The specific usage is as follows:


LPTSTR lpCommand = NULL;
TCHAR zCmd [MAX_PATH];
LpCommand = GetCommandLine ();
AnalyseCommandLine (lpCommand, zCmd );
LPTSTR lpCommand = NULL; TCHAR zCmd [MAX_PATH]; lpCommand = GetCommandLine (); AnalyseCommandLine (lpCommand, zCmd );

In theory, the link can be compiled. In fact, there are still many errors, mainly in terms of links. Because we use Windows APIs, we cannot use memset. In this case, we can call FillMemory (RtlFillMemory), ZeroMemory (RtlZeroMemory), and so on, in this case, a link error occurs during compilation, which is about the _ memset symbolic link. In fact, memset is not used. How does this error occur? In fact, Microsoft has redefined RtlFillMemory and other APIs and mounted them to the memset function. In order for us to compile smoothly, we need to do the following in our own header file.

# Undef RtlFillMemory
# Undef RtlZeroMemory
Extern "C" NTSYSAPI BOOL NTAPI
RtlFillMemory (VOID * Source1, DWORD Source2, BYTE Fill );
Extern "C" ntsysapi bool ntapi
Rtlzeromemory (pvoid destination, size_t length );
# Define memset (destination, fill, length) rtlfillmemory (destination), (length), (fill ))

# UNDEF duration # UNDEF rtlzeromemory extern "C" ntsysapi bool ntapi rtlfillmemory (void * source1, DWORD source2, byte fill); extern "C" ntsysapi bool ntapi destination (pvoid destination, size_t length); # define memset (destination, fill, length) rtlfillmemory (destination), (length), (fill ))

Next we will continue our compilation and there will be an error at the link.

Error lnk2001: external symbols that cannot be parsed _ imp _ initcommoncontrols @ 0
Error lnk2001: external symbols that cannot be parsed _ imp _ initcommoncontrolsex @ 4

After tracking, we found that the two functions initcommoncontrols and initcommoncontrolsex are exported by comctl32.dll. Refer to the online solution and add the Lib library.

# Include <commctrl. h>
# Pragma comment (Lib, "comctl32.lib ")

# Include # Pragma comment (lib, "comctl32.lib ")

The error persists! After careful searching, you can find the link in property configuration-> enter comctl32.lib in the additional dependencies, compile, and pass!

For more information about space allocation, see API functions such as HeapAlloc and HeapFree.

If you have any questions during the operation, please discuss them!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.