Javid
Javid

Reputation: 2945

Linker error after including fstream.h in KMDF driver

I'm a newbie in driver developing. This is my code:

#include <ndis.h>
#include <fwpmk.h>
#include <fwpvi.h>
#include <fwpsk.h>
#include <fstream.h>
#pragma comment(lib, "libcpmtd.lib")

extern "C" {

    NTSTATUS DriverEntry(
        _In_ struct _DRIVER_OBJECT *DriverObject,
        _In_ PUNICODE_STRING       RegistryPath
    )
    {
        ofstream s("D:\\somefile.txt");
        s << "driver initialized.";
        return 0;
    }
}

Produces the following list of errors:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "public: virtual __thiscall ios::~ios(void)" (??1ios@@UAE@XZ) referenced in function "public: void __thiscall ofstream::`vbase destructor'(void)" (??_Dofstream@@QAEXXZ) KMDF Driver1    D:\Projects\C++\KMDF Driver1\KMDF Driver1\Source.obj    1   
Error   LNK2019 unresolved external symbol "public: class ostream & __thiscall ostream::operator<<(char const *)" (??6ostream@@QAEAAV0@PBD@Z) referenced in function _DriverEntry@8 KMDF Driver1    D:\Projects\C++\KMDF Driver1\KMDF Driver1\Source.obj    1   
Error   LNK2019 unresolved external symbol "public: __thiscall ofstream::ofstream(char const *,int,int)" (??0ofstream@@QAE@PBDHH@Z) referenced in function _DriverEntry@8   KMDF Driver1    D:\Projects\C++\KMDF Driver1\KMDF Driver1\Source.obj    1   
Error   LNK2019 unresolved external symbol "public: virtual __thiscall ofstream::~ofstream(void)" (??1ofstream@@UAE@XZ) referenced in function "public: void __thiscall ofstream::`vbase destructor'(void)" (??_Dofstream@@QAEXXZ)  KMDF Driver1    D:\Projects\C++\KMDF Driver1\KMDF Driver1\Source.obj    1   
Error   LNK2001 unresolved external symbol "public: static int const filebuf::openprot" (?openprot@filebuf@@2HB)    KMDF Driver1    D:\Projects\C++\KMDF Driver1\KMDF Driver1\Source.obj    1   
Error   LNK1120 5 unresolved externals  KMDF Driver1    D:\Projects\C++\KMDF Driver1\Debug\KMDFDriver1.sys  1   

Upvotes: 0

Views: 209

Answers (1)

RbMm
RbMm

Reputation: 33754

this symbols (ios, ofstream)- from msvcrt.dll(or even if use static libc - it have dependencies from kernel32.dll for example) and cannot be used in kernel mode. you need remove "ostream s"- use DbgPrint or ZwCreateFile/ZwWriteFile for logging

Upvotes: 1

Related Questions