Fall 2022 - CS609 - 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Assignment No.

1 CS609
Submitted By BC190404087 (Husnain Iqbal)
Question 1:
Suppose we are going to create a new file named “vuSample.txt” on a hard disk using the
Windows CreateFile() API. Write the signature of this API by setting the parameters as:
1. The file is created for read/write both.
Answer:
FILE_ATTRIBUTES_NORMAL
2. The file is not required to be created for sharing.
Answer:
dwShareMode: 0 Signature That File will Not Be Shared
3. Security attributes should be default.
Answer:
IpsecurityAttributes
4. If the same name filename exists, then it is overwritten by the new file.
Answer:
ofstream myfile ("vuSample.txt");
myfile << "JUNAID.\n";
myfile << "JUNAID MALIK.\n";
myfile.close();
5. Set all other parameters as NULL.
Answer:
array_create(NULL,10);

Question 2:
Write a C++ program for getting the last error code generated during execution of a process,
convert this code into a user-understandable language format using the Windows
FormatMessage() API and display the message along with the error code.

Hints:
1. Include <everything.h> header file in your program and use the Windows API for
getting last error code.
2. Set the dwFlags parameter in FormatMessage() API as
“FORMAT_MESSAGE_ALLOCATE_BUFFER”
3. In language selection, set the parameter for system default language ID
4. Neglect the Error-reporting code. Marks will be deducted on including extra or irrelevant
code.

Answer:
CODE:
#include <windows.h>
#include <everything.h>
#include <strsafe.h>

void ErrorExit(LPTSTR lpszFunction)


{

LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );

lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) *
sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}

void main()
{

if(!GetProcessId(NULL))
ErrorExit(TEXT("GetProcessId"));
}

You might also like