EDIT : I seem to have solved the problem, even if I don’t know the particulars of why it worked. See the answer below this post.
I’m trying to compile the sample language monitor PJLMon, but a linker error is preventing the code from building successfully. I’d post the whole code, but it’s a bit much (most of it probably irrelevant to the question at hand), and is easily found online.
The error in question is: “1>pjlmon.obj : error LNK2019: unresolved external symbol _SetPort referenced in function _ClearPrinterStatusAndIniJobs”
The code in question refers to that found in pjlmon.c, and is shown below:
VOID
ClearPrinterStatusAndIniJobs(
__in PINIPORT pIniPort
)
{
PORT_INFO_3 PortInfo3;
if ( pIniPort->PrinterStatus ||
(pIniPort->status & PP_PRINTER_OFFLINE) ) {
pIniPort->PrinterStatus = 0;
pIniPort->status &= ~PP_PRINTER_OFFLINE;
ZeroMemory(&PortInfo3, sizeof(PortInfo3));
SetPort(NULL, pIniPort->pszPortName, 3, (LPBYTE)&PortInfo3); // LNK2019 HERE
}
SendJobLastPageEjected(pIniPort, ALL_JOBS, FALSE);
}
I have not modified the program logic in any way. I have modified some header files to be found locally (i.e. #include “winsplp.h” instead of ), but these files are otherwise unchanged, so I don’t think this is an issue. I’ll go ahead and show my defines and includes anyway, though, just in case I made a silly mistake:
#define USECOMM
#include "precomp.h"
#include "ntddpar.h"
#include <windows.h>
#undef SetPort
#include <WinReg.h>
#include <winioctl.h>
I have CLR turned off, and I’m not using precompiled headers.
Under Project->Properties->Linker->Input->Additional Dependencies, I added “winspool.lib; advAPI32.lib”. These files should contain references to the functions in winspool.h (which is included automatically with windows.h) and winreg.h.
The #undef SetPort code removes a macro which is set to change “SetPort” to “SetPortW” (unicode) or “SetPortA” (ascii). I don’t think this is a problem.
I’m a bit lost as to where to go from here. Any suggestions would be greatly appreciated!
SetPort() does not exist as a function in the Windows libraries. You can have SetPortA() for ANSI (really mutibyte) strings or SetPortW() for Wide (Unicode) strings. By using
#undef SetPortyou are preventing the preprocessor from replacing that call with the call to the correct variant of A or W, which is done based on the UNICODE proprocessor symbol.If you want to fix the error you either get rid of the #undef line (as you have done from your answer) or call the correct function manually as you will know whether you need to call the ANSI or Wide version depending on whether you are working with Unicode strings or not.