DYNLINK

Hi.

I have the following problem below. While I can compile this successfully as a program. I cannot as a link library. I have searched the forums and I know I am doing something wrong. I’m not sure what.

Could someone help me out?

n.

TARGETNAME=mylibrary
TARGETTYPE=DYNLINK
DLLBASE=0x2000000
DLLDEF=mylibrary.def

C_DEFINES=$(C_DEFINES) -DUNICODE -D_UNICODE -DNTAPILIBBUILD
LINKLIBS=$(SDK_LIB_PATH)\shell32.lib
INCLUDES=$(INCLUDES);$(IFSKIT_INC_PATH);$(DDK_INC_PATH);
TARGETLIBS=$(TARGETLIBS) $(IFSKIT_LIB_PATH)\fltLib.lib C:\WinSDK\Lib\Psapi.lib
SOURCES=mylibrary.c
BUFFER_OVERFLOW_CHECKS=0

1>mylibrary.obj : error LNK2019: unresolved external symbol __imp__CreateEventW@16 referenced in function _OpenPipe@8
1>errors in directory c:\development\fileprotector\development\dll
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol __imp__CreateEventW@16 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol __imp__SetNamedPipeHandleState@16 referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol __imp__SetNamedPipeHandleState@16 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol xxxxx@8 referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol xxxxx@8 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol xxxxx@0 referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol xxxxx@0 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol __imp__CreateFileW@28 referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol __imp__CreateFileW@28 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol _printf referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol _printf referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol __imp__MultiByteToWideChar@24 referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol __imp__MultiByteToWideChar@24 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol __imp__PeekNamedPipe@24 referenced in function _PeekReadPipe@4
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol __imp__PeekNamedPipe@24 referenced in function _PeekReadPipe@4
1>mylibrary.obj : error LNK2019: unresolved external symbol __imp__GetOverlappedResult@16 referenced in function _ReadPipe@12
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol __imp__GetOverlappedResult@16 referenced in function _ReadPipe@12
1>mylibrary.obj : error LNK2019: unresolved external symbol __imp__ReadFile@20 referenced in function _ReadPipe@12
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol __imp__ReadFile@20 referenced in function _ReadPipe@12
1>mylibrary.obj : error LNK2019: unresolved external symbol __imp__WriteFile@20 referenced in function _WritePipe@12
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol __imp__WriteFile@20 referenced in function _WritePipe@12
1>mylibrary.obj : error LNK2019: unresolved external symbol xxxxx@4 referenced in function _WritePipe@12
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol xxxxx@4 referenced in function _WritePipe@12
1>mylibrary.obj : error LNK2019: unresolved external symbol xxxxx@4 referenced in function _ClosePipe@4
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external symbol xxxxx@4 referenced in function _ClosePipe@4
1>c:\dev\src\mylibrary\objfre_wxp_x86\i386\mylibrary.dll : fatal error LNK1120: 13 unresolved externals
1>c:\dev\src\mylibrary\objfre_wxp_x86\i386\mylibrary.dll : error LNK1120: 13 unresolved externals

The TARGETLIBS does not contain kernel32.lib, advapi32.lib, etc.

Go look at most any sample in the DDK that links as a DLL. You will see
what typically is specified for a basic set of TARGETLIBS.

In general TARGETLIBS only get’s augmented by the system build procedure
with ntdll.lib for usermode code. You have to specify all the rest.

Did the SOURCES file for the program (exe) version of this specify the
libraries?

Good Luck,
Dave Cattley

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@hotmail.co.uk
Sent: Saturday, November 21, 2009 6:06 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] DYNLINK

Hi.

I have the following problem below. While I can compile this successfully as
a program. I cannot as a link library. I have searched the forums and I know
I am doing something wrong. I’m not sure what.

Could someone help me out?

n.

TARGETNAME=mylibrary
TARGETTYPE=DYNLINK
DLLBASE=0x2000000
DLLDEF=mylibrary.def

C_DEFINES=$(C_DEFINES) -DUNICODE -D_UNICODE -DNTAPILIBBUILD
LINKLIBS=$(SDK_LIB_PATH)\shell32.lib
INCLUDES=$(INCLUDES);$(IFSKIT_INC_PATH);$(DDK_INC_PATH);
TARGETLIBS=$(TARGETLIBS) $(IFSKIT_LIB_PATH)\fltLib.lib
C:\WinSDK\Lib\Psapi.lib
SOURCES=mylibrary.c
BUFFER_OVERFLOW_CHECKS=0

1>mylibrary.obj : error LNK2019: unresolved external symbol
__imp__CreateEventW@16 referenced in function _OpenPipe@8
1>errors in directory c:\development\fileprotector\development\dll
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol __imp__CreateEventW@16 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol
__imp__SetNamedPipeHandleState@16 referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol __imp__SetNamedPipeHandleState@16 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol
xxxxx@8 referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol xxxxx@8 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol
xxxxx@0 referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol xxxxx@0 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol
__imp__CreateFileW@28 referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol __imp__CreateFileW@28 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol _printf
referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol _printf referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol
__imp__MultiByteToWideChar@24 referenced in function _OpenPipe@8
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol __imp__MultiByteToWideChar@24 referenced in function _OpenPipe@8
1>mylibrary.obj : error LNK2019: unresolved external symbol
__imp__PeekNamedPipe@24 referenced in function _PeekReadPipe@4
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol __imp__PeekNamedPipe@24 referenced in function _PeekReadPipe@4
1>mylibrary.obj : error LNK2019: unresolved external symbol
__imp__GetOverlappedResult@16 referenced in function _ReadPipe@12
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol __imp__GetOverlappedResult@16 referenced in function _ReadPipe@12
1>mylibrary.obj : error LNK2019: unresolved external symbol
__imp__ReadFile@20 referenced in function _ReadPipe@12
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol __imp__ReadFile@20 referenced in function _ReadPipe@12
1>mylibrary.obj : error LNK2019: unresolved external symbol
__imp__WriteFile@20 referenced in function _WritePipe@12
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol __imp__WriteFile@20 referenced in function _WritePipe@12
1>mylibrary.obj : error LNK2019: unresolved external symbol
xxxxx@4 referenced in function _WritePipe@12
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol xxxxx@4 referenced in function _WritePipe@12
1>mylibrary.obj : error LNK2019: unresolved external symbol
xxxxx@4 referenced in function _ClosePipe@4
1>c:\dev\src\mylibrary\mylibrary.obj : error LNK2019: unresolved external
symbol xxxxx@4 referenced in function _ClosePipe@4
1>c:\dev\src\mylibrary\objfre_wxp_x86\i386\mylibrary.dll : fatal error
LNK1120: 13 unresolved externals
1>c:\dev\src\mylibrary\objfre_wxp_x86\i386\mylibrary.dll : error LNK1120: 13
unresolved externals


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

I can’t see why I didn’t see that.

Thankyou very much. I guess I was tired.

I am wondering, I have the below code. The problem I have is, if I compile this with a main and run the application, the program works without a problem. I have - am - compiling this as a dll through WDK 7600. When I call each function I get a GetLastError of ERROR_IO_INCOMPLETE.

Does anyone know what I am doing wrong?

#pragma warning(disable:4995)

/* List of included headers required. */
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winioctl.h>
#include <string.h>
#include <crtdbg.h>
#include <assert.h>
#include <fltuser.h>
#include <wchar.h>
#include <strsafe.h>
#include <conio.h>

#define MAXIMUM_FILE_LENGTH 1024 /* Maximum Filename Length. /
#define DEFAULT_RQ 12 /
Maximum number of Requests. /
#define DEFAULT_THREADS 12 /
Default number of Threads. /
#define DEFAULT_MX_THREADS 12 /
Maximum number of Threads. /
#define BUFSIZE 65535

int OpenPipe(DWORD PipeNumber, PCHAR PipeName);
int PeekReadPipe(DWORD PipeNumber);
int ReadPipe(DWORD PipeNumber, PTCHAR PipeRead, DWORD ReadLength);
int WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength);
int ClosePipe(DWORD PipeNumber);

HANDLE hPipe[DEFAULT_THREADS];

int WriteToLog(char
uString)
{
FILE* pFile;
if((pFile = fopen(“logfile.log”, “a+”)) == NULL) return -1;
fprintf(pFile, “%s”, uString);
fclose(pFile);

return 0;
}

int __stdcall OpenPipe(DWORD PipeNumber, PCHAR PipeName)
{
DWORD dwMode;
BOOL fSuccess = FALSE;
WCHAR wBuffer[MAXIMUM_FILE_LENGTH];
UCHAR ErrorMessage[1024];

MultiByteToWideChar(CP_ACP, 0, PipeName, -1, wBuffer, strlen(PipeName));
wBuffer[strlen(PipeName)] = ‘\0’;

while (1)
{
sprintf((char*) ErrorMessage, “%ws \n”, wBuffer);
//WriteToLog(ErrorMessage);

hPipe[PipeNumber] = CreateFile(wBuffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (hPipe[PipeNumber] != INVALID_HANDLE_VALUE) break;
if (GetLastError() != ERROR_PIPE_BUSY)
{
sprintf((char*) ErrorMessage, “error.\nGetLastError() != ERROR_PIPE_BUSY. (probably means the service was not running.)\n”);
// WriteToLog(ErrorMessage);
return 1;
}

if (!WaitNamedPipe(wBuffer, 20000))
{
sprintf((char*) ErrorMessage, “error.\nCould not open pipe: 20 second wait timed out.”);
// WriteToLog(ErrorMessage);
return 1;
}
}

dwMode = PIPE_TYPE_BYTE;
fSuccess = SetNamedPipeHandleState(hPipe[PipeNumber], &dwMode, NULL, NULL);
if (!fSuccess)
{
sprintf((char*) ErrorMessage, “error.\nSetNamedPipeHandleState unfortunately failed.\n”);
// WriteToLog(ErrorMessage);
return 1;
}

sprintf((char*) ErrorMessage, “ok.\n”);
// WriteToLog(ErrorMessage);
return 0;
}

int__stdcall PeekReadPipe(DWORD PipeNumber)
{
DWORD cbRead;
PeekNamedPipe(hPipe[PipeNumber], NULL, MAXIMUM_FILE_LENGTHsizeof(TCHAR), NULL, &cbRead, NULL);
if(cbRead > 0) return 0;
else return 1;
}

int ReadPipe(DWORD PipeNumber, PTCHAR PipeRead, DWORD ReadLength)
{
BOOL fSuccess = FALSE;
UCHAR ErrorMessage[1024];
OVERLAPPED hEventOverlapped;

WriteToLog(“number0.\n”);
//memset(hEventOverlapped, 0, sizeof(hEventOverlapped));
hEventOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
hEventOverlapped.Offset = 0;

ReadLength = 0;
fSuccess = ReadFile(hPipe[PipeNumber], PipeRead, MAXIMUM_FILE_LENGTH
sizeof(TCHAR), NULL, &hEventOverlapped);
if(GetLastError() != ERROR_IO_PENDING)
{
fSuccess = GetOverlappedResult(hPipe[PipeNumber], &hEventOverlapped, &ReadLength, FALSE);
sprintf((char*) ErrorMessage, “Read. %d %s %d.\n”, ReadLength, PipeRead, GetLastError());
WriteToLog(ErrorMessage);
if(ReadLength > 0)
{
sprintf((char*) ErrorMessage, “Read. %s.\n”, PipeRead);
WriteToLog(ErrorMessage);
return 0;
}
}
else fSuccess = GetOverlappedResult(hPipe[PipeNumber], &hEventOverlapped, &ReadLength, FALSE);
WriteToLog(“number1.\n”);

return 1;
}

int __stdcall WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength)
{
BOOL fSuccess = FALSE;
WCHAR wBuffer[MAXIMUM_FILE_LENGTH];
DWORD cbWritten;
OVERLAPPED hEventOverlapped;

//memset(hEventOverlapped, 0, sizeof(hEventOverlapped));
hEventOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
hEventOverlapped.Offset = 0;

MultiByteToWideChar(CP_ACP, 0, PipeWrite, -1, wBuffer, strlen(PipeWrite));
wBuffer[strlen(PipeWrite)] = ‘\0’;

fSuccess = WriteFile(hPipe[PipeNumber], wBuffer, (lstrlen(wBuffer)+1)*sizeof(TCHAR), &cbWritten, NULL);

if (!fSuccess) return 1;
else return 0;
}

int__stdcall ClosePipe(DWORD PipeNumber)
{
CloseHandle(hPipe[PipeNumber]);

return 0;
}</conio.h></strsafe.h></wchar.h></fltuser.h></assert.h></crtdbg.h></string.h></winioctl.h></stdio.h></stdlib.h></windows.h>

I should say, that PeekReadPipe is successful, but ReadPipe returns the ERROR_IO_INCOMPLETE on line:
sprintf((char*) ErrorMessage, "Read. %d %s %d.\n", ReadLength, PipeRead,
GetLastError());

Previous Message:

I can't see why I didn't see that.

Thankyou very much. I guess I was tired.

I am wondering, I have the below code. The problem I have is, if I compile this
with a main and run the application, the program works without a problem. I have

  • am - compiling this as a dll through WDK 7600. When I call each function I get
    a GetLastError of ERROR_IO_INCOMPLETE.

Does anyone know what I am doing wrong?

#pragma warning(disable:4995)

/* List of included headers required. */
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winioctl.h>
#include <string.h>
#include <crtdbg.h>
#include <assert.h>
#include <fltuser.h>
#include <wchar.h>
#include <strsafe.h>
#include <conio.h>

#define MAXIMUM_FILE_LENGTH 1024 /* Maximum Filename Length. /
#define DEFAULT_RQ 12 /
Maximum number of Requests. /
#define DEFAULT_THREADS 12 /
Default number of Threads. /
#define DEFAULT_MX_THREADS 12 /
Maximum number of Threads. /
#define BUFSIZE 65535

int OpenPipe(DWORD PipeNumber, PCHAR PipeName);
int PeekReadPipe(DWORD PipeNumber);
int ReadPipe(DWORD PipeNumber, PTCHAR PipeRead, DWORD ReadLength);
int WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength);
int ClosePipe(DWORD PipeNumber);

HANDLE hPipe[DEFAULT_THREADS];

int WriteToLog(char
uString)
{
FILE* pFile;
if((pFile = fopen("logfile.log", "a+")) == NULL) return -1;
fprintf(pFile, "%s", uString);
fclose(pFile);

return 0;
}

int __stdcall OpenPipe(DWORD PipeNumber, PCHAR PipeName)
{
DWORD dwMode;
BOOL fSuccess = FALSE;
WCHAR wBuffer[MAXIMUM_FILE_LENGTH];
UCHAR ErrorMessage[1024];

MultiByteToWideChar(CP_ACP, 0, PipeName, -1, wBuffer, strlen(PipeName));
wBuffer[strlen(PipeName)] = '\0';

while (1)
{
sprintf((char*) ErrorMessage, "%ws \n", wBuffer);
//WriteToLog(ErrorMessage);

hPipe[PipeNumber] = CreateFile(wBuffer, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (hPipe[PipeNumber] != INVALID_HANDLE_VALUE) break;
if (GetLastError() != ERROR_PIPE_BUSY)
{
sprintf((char*) ErrorMessage, "error.\nGetLastError() != ERROR_PIPE_BUSY.
(probably means the service was not running.)\n");
// WriteToLog(ErrorMessage);
return 1;
}

if (!WaitNamedPipe(wBuffer, 20000))
{
sprintf((char*) ErrorMessage, "error.\nCould not open pipe: 20 second wait
timed out.");
// WriteToLog(ErrorMessage);
return 1;
}
}

dwMode = PIPE_TYPE_BYTE;
fSuccess = SetNamedPipeHandleState(hPipe[PipeNumber], &dwMode, NULL, NULL);
if (!fSuccess)
{
sprintf((char*) ErrorMessage, "error.\nSetNamedPipeHandleState unfortunately
failed.\n");
// WriteToLog(ErrorMessage);
return 1;
}

sprintf((char*) ErrorMessage, "ok.\n");
// WriteToLog(ErrorMessage);
return 0;
}

int__stdcall PeekReadPipe(DWORD PipeNumber)
{
DWORD cbRead;
PeekNamedPipe(hPipe[PipeNumber], NULL, MAXIMUM_FILE_LENGTHsizeof(TCHAR),
NULL, &cbRead, NULL);
if(cbRead > 0) return 0;
else return 1;
}

int ReadPipe(DWORD PipeNumber, PTCHAR PipeRead, DWORD ReadLength)
{
BOOL fSuccess = FALSE;
UCHAR ErrorMessage[1024];
OVERLAPPED hEventOverlapped;

WriteToLog("number0.\n");
//memset(hEventOverlapped, 0, sizeof(hEventOverlapped));
hEventOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
hEventOverlapped.Offset = 0;

ReadLength = 0;
fSuccess = ReadFile(hPipe[PipeNumber], PipeRead,
MAXIMUM_FILE_LENGTH
sizeof(TCHAR), NULL, &hEventOverlapped);
if(GetLastError() != ERROR_IO_PENDING)
{
fSuccess = GetOverlappedResult(hPipe[PipeNumber], &hEventOverlapped,
&ReadLength, FALSE);
sprintf((char*) ErrorMessage, "Read. %d %s %d.\n", ReadLength, PipeRead,
GetLastError());
WriteToLog(ErrorMessage);
if(ReadLength > 0)
{
sprintf((char*) ErrorMessage, "Read. %s.\n", PipeRead);
WriteToLog(ErrorMessage);
return 0;
}
}
else fSuccess = GetOverlappedResult(hPipe[PipeNumber], &hEventOverlapped,
&ReadLength, FALSE);
WriteToLog("number1.\n");

return 1;
}

int __stdcall WritePipe(DWORD PipeNumber, PCHAR PipeWrite, DWORD WriteLength)
{
BOOL fSuccess = FALSE;
WCHAR wBuffer[MAXIMUM_FILE_LENGTH];
DWORD cbWritten;
OVERLAPPED hEventOverlapped;

//memset(hEventOverlapped, 0, sizeof(hEventOverlapped));
hEventOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
hEventOverlapped.Offset = 0;

MultiByteToWideChar(CP_ACP, 0, PipeWrite, -1, wBuffer, strlen(PipeWrite));
wBuffer[strlen(PipeWrite)] = '\0';

fSuccess = WriteFile(hPipe[PipeNumber], wBuffer,
(lstrlen(wBuffer)+1)*sizeof(TCHAR), &cbWritten, NULL);

if (!fSuccess) return 1;
else return 0;
}

int__stdcall ClosePipe(DWORD PipeNumber)
{
CloseHandle(hPipe[PipeNumber]);

return 0;
}</conio.h></strsafe.h></wchar.h></fltuser.h></assert.h></crtdbg.h></string.h></winioctl.h></stdio.h></stdlib.h></windows.h>

xxxxx@hotmail.co.uk wrote:

I can’t see why I didn’t see that.

Thankyou very much. I guess I was tired.

I am wondering, I have the below code. The problem I have is, if I compile this with a main and run the application, the program works without a problem. I have - am - compiling this as a dll through WDK 7600. When I call each function I get a GetLastError of ERROR_IO_INCOMPLETE.

Does anyone know what I am doing wrong?

You are passing FALSE as the bWait parameter to GetOverlappedResult.
That means if there is no data waiting on the pipe, you get
ERROR_IO_INCOMPLETE. If you want the read to wait until data is
available, you should set bWait to TRUE.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.