A trap in C language:

Source: Internet
Author: User

The actual problem encountered was that I copied a piece of code from MSDN, which was written in C. The code was compiled and the execution crashed,

# Include
 
  
// Test. c uses Unicode to compile and crash void main () {STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory (& si, sizeof (si); si. cb = sizeof (si); ZeroMemory (& pi, sizeof (pi); if (! CreateProcess (NULL, // No module name (use command line) "calc.exe", // Command lineNULL, // Process handle not inheritableNULL, // Thread handle not inheritableFALSE, // Set handle inheritance to FALSE0, // No creation flagsNULL, // Use parent's environment blockNULL, // Use parent's starting directory & si, // Pointer to STARTUPINFO structure & pi) // Pointer to PROCESS_INFORMATION structure) WaitForSingleObject (pi. hProcess, INFINITE); // Close process and thread handles. closeHandle (pi. hProcess); CloseHandle (pi. hThread );}
 

The above code can be compiled in unicode c mode. If the code crashes during runtime, the compiler will report a warning, which is not an error.

Test. c (13): warning C4133: 'function': incompatible types-from 'Char [9] 'to 'lpwstr'

The second parameter of CreateProcessW must be LPWSTR. If it is forcibly converted and compiled in c ++ mode, an error is returned.

Test. cpp (21): error C2664: 'createprocessw': cannot convert parameter 2 from 'const char [9] 'to 'lpwstr'

Related Article

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.