Boost Serialization MSVC 2015 do not compile in DEBUG mode - visual-c++

By some reason MSVC DO NOT compile boost serialization example with the following code:
class MyName
{
public:
MyName(std::string _name, std::string _family_name)
:name{ _name }, family_name{ _family_name }
{ }
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{ ar & name; ar & family_name; } std::string name; std::string family_name;
};
int main()
{
// create and open a character archive for output
std::stringstream ofs;
// save data to archive
{
MyName my_name("MyName", "FamilyName");
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << my_name;
// archive and stream closed when destructors are called
}
// save data to archive
{
MyName my_name("afsf", "dgsass");
boost::archive::text_iarchive oa(ofs);
// write class instance to archive
oa >> my_name;
// archive and stream closed when destructors are called
}
return 0;
}
I get the follwing error:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall boost::archive::archive_exception::archive_exception(enum boost::archive::archive_exception::exception_code,char const *,char const *)" (??0archive_exception#archive#boost##QAE#W4exception_code#012#PBD1#Z) referenced in function "protected: void __thiscall boost::archive::basic_text_iprimitive<class std::basic_istream<char,struct std::char_traits<char> > >::load<unsigned int>(unsigned int &)" (??$load#I#?$basic_text_iprimitive#V?$basic_istream#DU?$char_traits#D#std###std###archive#boost##IAEXAAI#Z) cpp11_cpp14_cpp17 D:\Projects_Programing\__Testing\cpp11_cpp14_cpp17\cpp11_cpp14_cpp17\cpp11_cpp14_cpp17.obj 1
But when I compiled it in release mode.
I have read that it could happen due to MSVC STRICT mode, but I have tried and it does not work neither.
Have anybody got such error ?

I have figured out the reason of this error.
It happens when I tried to compile with flag /Za (means zero extension from MSVC for C++).
When I removed this flag my code compiles succesfully.
#lakeweb Thank you for your help and support !!
Unfortunately I do not understand why some extensions from MSVC does allow to compile Boost, but without extentions it does not compile. It is very strange !!
Maybe it is either the bug on Boost side or on MSVC side.
Any assumption ?

Related

LNK2019 unable to use GetStagedPackageOrigin()

I have been trying to use the GetStagedPackageOrigin() to get the origin of a specific application in my system.
But my program is not compiling because of an unresolved external error.
Error LNK2019 : unresolved external symbol __imp__GetStagedPackageOrigin#8 referenced in function "void __cdecl check(wchar_t const *)" (?check##YAXPB_W#Z)
Here is my code:
void check(__in PCWSTR fullName)
{
PackageOrigin origin;
LONG rc = GetStagedPackageOrigin(fullName, &origin);
if (rc == ERROR_SUCCESS)
{
static PCWSTR originstring[] = {L"Unknown",L"Unsigned",L"Inbox",L"Store",L"DeveloperUnsigned",L"DeveloperSigned",L"LineOfBusiness" };
PCWSTR str = originstring[origin];
wcout << str << endl;
}
}
Why does this error keep coming and is there any way to resolve that?
I used dumpbin command in the visual studio command prompt and that function was not being exported from kernel32.dll. The function was not present in the dll in the first place.
I think that is why the error occured.

Can object pointers be passed between CLR and native C++?

I have been trying to pass a class pointer from a native DLL to CLR. I am not so successful with it. I get to the point it returns the pointer using void * and later converting it to ClassType * using IntrPtr and ToPointer(). But the minute I try to access its member methods, its not able to get the old property values. I get instead 0 and some random values.
I have even tried to use VC100 as the toolset for both the CLR and native dll. Still I get the value "Error reading characters of string".
I tried to google and can't find much info on passing object pointers from native to CLR. I have referred to even the step-by-step available in code project.
Edited to add this:
I have a native dll where a function that returns object pointer
Native.dll:
#if defined EXPORTDLL // inside DLL
#define DLL_EXPORT __declspec(dllexport)
#else // outside DLL
#define DLL_EXPORT __declspec(dllimport)
#endif
.....
DLL_EXPORT void* SomeMethod(uint16_t num1, const char * str1, const char * str2)
{
Obj1 obj(...);
...
return (void*)&obj; // <- at this point it is still correct
}
....
class DLL_EXPORT Obj1{
....
const std::string var1;
const std::string var2;
const std::string var3;
....
DLL_EXPORT strct1 memberFunction1()
{
// do something with the variables
// here when its called by managed code, the var1, var2, var3 shows random values and bad ptr..
}
...
}
And later in managed code clr I call the memberFunct1 using the return pointer.
[DllImport("Native.dll", EntryPoint = "?SomeMethod##YAPAVstruct1#someSpace##GV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##0#Z", CallingConvention = CallingConvention::Cdecl)]
IntPtr * SomeMethod(uint16_t num1, [In, MarshalAs(UnmanagedType::LPStr)]String^ str1, [In, MarshalAs(UnmanagedType::LPStr)] String^ str2);
.....
String^ str1 = gcnew String("1223568");
String^ str2 = gcnew String("1.2.3.5");
Obj1 *objptr = (Obj1*)(SomeMethod(1,str1, str2)).ToPointer();
// <- at this point the objptr properties are showing garbage
objptr->memberFunction1();
I use this obj1 pointer to call a member method but in the member method the member values are showing as bad ptr.
Can someone point me in the right direction please?
Thanks in advance.
I have added an additional method in my Native dll.
void DLL_EXPORT SomeMethod2(int i1, const char* var1, const char* var2, Obj1* retval);
And called it from my CLR project
[DllImport("Native.dll", EntryPoint = "?SomeMethod2##YAXHPBD0PAVObj1###Z", CallingConvention = CallingConvention::Cdecl)]
void SomeMethod2(int i1, [MarshalAs(UnmanagedType::LPStr)]String^ var1, [MarshalAs(UnmanagedType::LPStr)]String^ var2, Obj1* retval);
....
Obj1 * testPtr3;
SomeMethod2(1, (char*)(void*)Marshal::StringToHGlobalAnsi(str1), (char*)(void*)Marshal::StringToHGlobalAnsi(str2), testPtr3);
SomeMethod2(1, str1, str2, testPtr3);
Obj1 testPtr2 = Obj1(1, (char*)(void*)Marshal::StringToHGlobalAnsi(str1), (char*)(void*)Marshal::StringToHGlobalAnsi(str2));
But I get link errors? I have already linked it??!!??
error LNK2028: unresolved token (0A000356) "public: __thiscall Obj1::Obj1(int,char const *,char const *)" (??0Obj1##$$FQAE#HPBD0#Z) referenced in function "int __cdecl main(void)" (?main##$$HYAHXZ)
error LNK2028: unresolved token (0A00035B) "void __cdecl SomeMethod2(int,char const *,char const *,class Obj1 *)" (?SomeMethod2##$$FYAXHPBD0PAVObj1###Z) referenced in function "int __cdecl main(void)" (?main##$$HYAHXZ)
error LNK2019: unresolved external symbol "public: __thiscall Obj1::Obj1(int,char const *,char const *)" (??0Obj1##$$FQAE#HPBD0#Z) referenced in function "int __cdecl main(void)" (?main##$$HYAHXZ)
error LNK2019: unresolved external symbol "void __cdecl SomeMethod2(int,char const *,char const *,class Obj1 *)" (?SomeMethod2##$$FYAXHPBD0PAVObj1###Z) referenced in function "int __cdecl main(void)" (?main##$$HYAHXZ)
You do not use C++/CLI in this way!
The whole point of C++/CLI is to act as a glue between the .NET world and the native world.
What you do instead is to:
Include the C++ headers for SomeMethod and class Obj1 in the C++/CLI source file and
have the C++/CLI assembly reference the Native.dll project
Then, you can simply use it as you would normally in C++ - convert any CLR types to native representation first (System::String can be converted by MFC CString if you include MFC):
String^ str1 = gcnew String("1223568");
String^ str2 = gcnew String("1.2.3.5");
Obj1 *objptr = SomeMethod(1, CString(str1), CString(str2));
As for why you see garbage in the debugger: It is probably because you return the address of a local object:
DLL_EXPORT void* SomeMethod(uint16_t num1, const char * str1, const char * str2)
{
Obj1 obj(...);
...
return (void*)&obj; // <- at this point it is still correct
}
In this case, objwill be cleaned up (its D'tor invoked and its stack memory recycled) as soon as you return from SomeMethod.
Either return the object by value
or new up the object and return the pointer. (though that is leak-risking code)

VS2012 WindowsApplicationForDrivers8.0 can not include trd lib

#pragma comment(lib, "libmcrypt.lib")
When the platform is Visual Studio 2012 (v110), it is OK. But, when platform is WindowsApplicationForDrivers8.0, it reports error LNK2019
When I include OpenSSL in my project, I'm getting the same error.
From the MSDN.
The following sample generates LNK2019:
// LNK2019.cpp
// LNK2019 expected
extern char B[100]; // B is not in avilable to the linker
int main() {
B[0] = ' ';
}
LNK2019 can also occur when you declare but do not define a static data member. The following sample generates LNK2019:
// LNK2019b.cpp
// LNK2019 expected
struct C {
static int s;
};
// Uncomment the following line to resolve.
// int C::s;
int main() {
C c;
C::s = 1;
}
Please go to the MSDN link for full explanation
http://msdn.microsoft.com/en-us/library/799kze2z(v=vs.80).aspx

Unknown symbol when loading my own kernel module

The following code (pasted last), taken mostly from here, is a very simple kernel module which acts as a keylogger. I can get it to compile and produce a .ko just fine, but when I try to load it, I get the following errors in dmesg:
[ 790.833828] keylogger: Unknown symbol unregister_keyboard_notifier (err 0)
[ 790.833846] keylogger: Unknown symbol register_keyboard_notifier (err 0)
I did not build my kernel from source, but am using the stock kernel provided with archlinux. I did install the kernel-headers package to get the module to compile, however.
So my question is: Are these two symbols really not found in my installed kernel? And if they are, why aren't they linking(?) correctly?
I can find evidence that the symbols are present. Firstly, I can see the symbols in /proc/kallsyms. Also, when I do nm /usr/src/vmlinux I can also see these two symbols. Are they not the same?
Module code:
#include <linux/module.h> /* Needed by all modules */
#include <linux/keyboard.h>
EXPORT_SYMBOL_NOVERS(unregister_keyboard_notifier);
EXPORT_SYMBOL_NOVERS(register_keyboard_notifier);
int hello_notify(struct notifier_block *nblock, unsigned long code, void *_param) {
struct keyboard_notifier_param *param = _param;
struct vc_data *vc = param->vc;
int ret = NOTIFY_OK;
if (code == KBD_KEYCODE) {
printk(KERN_DEBUG "KEYLOGGER %i %s\n", param->value, (param->down ? "down" : "up"));
}
}
static struct notifier_block nb = {
.notifier_call = hello_notify
};
static int hello_init(void)
{
register_keyboard_notifier(&nb);
return 0;
}
static void hello_release(void)
{
unregister_keyboard_notifier(&nb);
}
module_init(hello_init);
module_exit(hello_release);
I needed to add the following to my module source:
MODULE_LICENSE("GPL");

error LNK2019: unresolved external symbol __imp__debugf referenced in function "int __cdecl fld_new

I am upgrading my project from VS 6 to VS 2010, while building in release mode, I am facing the below error.
1>Creating library .\Release\JfFrpF32.lib and object .\Release\JfFrpF32.exp>
1>FLD_.obj : error LNK2019: unresolved external symbol __imp__debugf referenced in function "int __cdecl fld_new(char *,unsigned char,unsigned char,short,char,char,unsigned char,short,char,double,double,short,char *,char,short,short)" (?fld_new##YAHPADEEFDDEFDNNF0DFF#Z)
1>Release/JfFrpF32.dll : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
Please help me .. thanks in advance..
Common problems that cause LNK2019 include:
The declaration of the symbol contains a spelling mistake, such that,
it is not the same name as the definition of the symbol.
A function was used but the type or number of the parameters did not
match the function definition.
The calling convention (__cdecl, __stdcall, or __fastcall) differs on
the use of the function declaration and the function definition.
Symbol definitions are in a file that was compiled as a C program and
symbols are declared in a C++ file without an extern "C" modifier. In
that case, modify the declaration.
For More Information See Here
In my case, even though I used extern "C", I got the unresolved symbol error.
The hpp was
extern "C"
{
class A
{
public:
void hi();
};
A* a;
DECLDIR int Connect();
}//extern
and the cpp was
#include "DatabasePlugin.hpp"// Include our header, must come after #define DLL_EXPORT
extern "C" // Get rid of name mangling
{
DECLDIR int Connect()
{
a = new A();
a->hi();
return 0;
}//Connect
}//extern
The problem was that I had not created an implementation for the hi() function. Adding it solved the problem. Like this:
extern "C" // Get rid of name mangling
{
void A::hi() {}
DECLDIR int Connect()
{
a = new A();
a->hi();
return 0;
}//Connect
}//extern
Having to declare Hi() before Connect() may also be significant.

Resources