Error trying to use function EnumPrinters()[C - WIN32 API]
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define WIN_32_LEAN_AND_MEAN
#include <windows.h>
#include <strsafe.h>
#define FILE_NAME "file.txt"
void ErrorExit(LPTSTR lpszFunction)
{
/* Retrieve the system error message for the last-error code */
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);
}
int main( )
{
BOOL returnValue = FALSE;
PBYTE printerInfo;
LPDWORD bytesCopied = NULL;
LPDWORD bytesNeeded = NULL;
FILE * newFile = NULL;
newFile = fopen(FILE_NAME, "w+");
printerInfo = malloc(sizeof(PRINTER_INFO_4));
if ( printerInfo == NULL )
{
perror("Malloc");
exit(1);
}
if ( newFile == NULL )
{
perror("Creating file");
exit(1);
}
returnValue = EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 4, printerInfo, sizeof(PRINTER_INFO_4), bytesNeeded, bytesCopied );
if ( !returnValue )
{
ErrorExit(TEXT("EnumPrinters"));
free(printerInfo);
exit(1);
}
fclose(newFile);
if ( remove(FILE_NAME) ) perror("Deleting file");
free(printerInfo);
return 0;
}
I decided to try to make my own code for printing a text document on the local printer. I've written some code to create a temporary text file( I haven't gotten to the actual printing yet, so I'm leaving it empty) also. My problem is EnumPrinters fails with error 1780 :
"A null reference was passed to the stub"
I have no idea what that is talking about, since I don't understand what it refers to when it says "stub". Can someone help me understand what's wrong with this code?