UInt32x32To64 identifier not found - c++

I am coding in C++ and i am importing a C# class library using
#import "C:\abc\abc.tlb"
using namespace XYZ;
When i do this i get an error in the file comutil.h
the error is
UInt32x32To64 identifier not found.I don't know i get this when i try to import a tlb file.
Can anyone help me in this matter?
This is where the portion of comutil.h
static HRESULT UIntMult(UINT uMultiplicand, UINT uMultiplier, UINT *puResult)
{
ULONGLONG ull64Result = UInt32x32To64(uMultiplicand, uMultiplier);
if(ull64Result <= INTSAFE_UINT_MAX)
{
*puResult = (UINT)ull64Result;
return S_OK;
}
return INTSAFE_E_ARITHMETIC_OVERFLOW;
}
Thanks

UInt32x32To64() is a macro defined in the Windows header files. You need to #include <Windows.h> to gain access to it.
Hans's comment is spot on. If you have HRESULT and UINT then presumably you have Windows.h. In which case the lack of the macro is presumably because the architecture conditional has not been defined.

Related

Unresolved external symbol CLRCreateInstance

I wrote some native C++ code with the help of the Internet that loads the .NET Runtime and invokes a method that has the signature: public static int MethodNameHere(String pwzArgument) from a class in a managed assembly.
However, I can't seem to compile the code using Visual Studio into a native DLL (64-bit), because there seems to be a linking issue with CLRCreateInstance(), even though I am including "MetaHost.h" in the .cpp source file.
Here is the entire code:
#include "MetaHost.h"
extern "C" {
__declspec(dllexport) DWORD __stdcall CallManagedMethod(LPCWSTR managedDLLPath, LPCWSTR classPathInAssembly, LPCWSTR methodName, LPCWSTR parameter) {
// Bind to the CLR runtime..
ICLRMetaHost* pMetaHost = nullptr;
CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*) &pMetaHost);
ICLRRuntimeInfo* pRuntimeInfo = nullptr;
pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*) &pRuntimeInfo);
ICLRRuntimeHost* pClrRuntimeHost = nullptr;
pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*) &pClrRuntimeHost);
pClrRuntimeHost->Start();
// Okay, the CLR is up and running in this (previously native) process.
// Now call a method on our managed C# class library.
DWORD dwRet = 0;
pClrRuntimeHost->ExecuteInDefaultAppDomain(managedDLLPath, classPathInAssembly, methodName, parameter, &dwRet);
// Don't forget to clean up.
pClrRuntimeHost->Release();
pRuntimeInfo->Release();
pMetaHost->Release();
return dwRet;
}
}
Any help?
A .h file does not solve a linking problem, it merely adds them. The MSDN article you linked to fumbles the usual way that include and link hints are documented, "Included as a resource" is quite unhelpful. In C++ you have to link mscoree.lib to get the symbol resolved. It is the import library for mscoree.dll and included in the SDK.
Simplest way is to add #pragma comment(lib, "mscoree.lib") after your #include.

C++ Classes: Conceptual Class Doppelganger: LNK2005

Sheesh. I've run into the infamous LNK2005 error when defining my class and I can't seem to place the issue.
(I'm tearing apart an equally infamous singleton of mine to reflect well, organization.) The initial said singleton was coded... ...in such a divergent, brilliant way... as to avoid all of the C++ OPP principles and concepts I was ignorant to at the time, but it worked! ....Somehow. Though some fluke of the simplest C++ concepts it did. Now I need organization, compilation speed, and advanced structuring techniques to make it work fast, and- you get it.
A-Anyway. After splitting it up, and having to rewrite some, I've noticed a necessity. I must declare multiple .cpp files simply because the compiler is irked massively by double declarations, and the usual header class definitions.
Additionally I've used preprocessor directives accordingly as applicable. But something still remains wrong.
Note(Edit): I've rewritten the question to provide error given.
Consider:
D3D.h
#include "Infinity.h"
class Direct3D :
public Infinity
{
public:
Direct3D();
~Direct3D();
IDXGISwapChain *Swapchain; //Display modes.
static ID3D11Device *Device;
static ID3D11DeviceContext *DeviceContext;
static ID3D11RenderTargetView *RenderTargetView;
void D3D_Start(float width, float height);
void D3D_Render();
void D3D_Terminate();
void ViewPort(float Height, float Width, float MaxDepth, float MinDepth, float TopLeftX, float TopLeftY);
}Direct3D;
...and Windows.h
#include "Infinity.h"
class Windows :
public Infinity
{
public:
Windows();
~Windows();
bool DisplayWindow(int width, int height, HINSTANCE hInstance);
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
}Windows;
Finally, Infinity.h
#pragma once
class Infinity{
public:
Infinity();
~Infinity();
static HWND hWnd;
};
Whereas all implementations are in their respective .cpp files. Besides #pragma, I've used #ifndef ... #endif. I suspect that I may be inadvertently calling a kind of implementation by auto-initializing the classes in their header files. But it looks insanely kosher, and allows me to declare function members as:
Direct3D.D3D_Start() without stating a static member, Direct3D::D3D_Start().
Should my headers all be static?
Edit: Below, the .cpp file:
#include "stdafx.h"
#include "Infinity.h"
#include "Windows.h"
#include "Direct3D.h"
MSG msg;
float width = 1024;
float height = 768;
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
windows.DisplayWindow(1280, 900, hInstance);
direct3D.D3D_Start(width, height);
direct3D.ViewPort(height, width, 1.0f, 0.0f, 0, 0);
while (WM_QUIT != msg.message){
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
direct3D.D3D_Render();
}
}
direct3D.D3D_Terminate();
return msg.wParam;
}
*Update the .cpp has been changed to show Niall's solution.
Edit:
Question:
Am I getting the LNK2005 issue because I auto-initialized my classes in their header files, considering the solution at Stack Overflow question:
VS 2010 C++ LNK2005 errors while using #pragma once and #ifndef
Which doesn't seem to work based my understanding of the solution.
Compiler
VS2013 returns:
Error 1 error LNK2005: "class Direct3D Direct3D" (?Direct3D##3V0#A) already defined in Direct3D.obj C:\Users\InfinityMachine\documents\visual studio 2013\Projects\Win32Project3\Win32Project3\Win32Project3.obj Win32Project3
Error 2 error LNK2005: "class Windows Windows" (?Windows##3V0#A) already defined in Win32Project3.obj C:\Users\InfinityMachine\documents\visual studio 2013\Projects\Win32Project3\Win32Project3\Windows.obj Win32Project3
Error 3 error LNK1169: one or more multiply defined symbols found C:\Users\InfinityMachine\documents\visual studio 2013\Projects\Win32Project3\Debug\Win32Project3.exe 1 1 Win32Project3
You declare class Direct3D... } Direct3D; and variable of the same name immediately. This would be the cause of such an error.
A variable of type Direct3D, named Direct3D (or otherwise) will be in each translation unit that includes that header, the same applies for Windows.
One possible resolution is to remove the variable declaration, another would be to move it and make it static, or in an anonymous namespace. Alternates include extern or implementing a singleton; which may be closer to the original intent.
Based on further discussions, an appropriate solution here is;
class Direct3D {};
extern Direct3D direct3D;
Then in the one of the implementation files
Direct3D direct3D;
This then declares the object as extern and provides a single instance of it.

Not finding function using GetProcAddress() C++ VBexpress 13

Okay so I'm coming dangerously close to a repost here but my situation is a little bit different than the numerous other posters about this function. I am interfacing with a DLL that was written way back in the day and all I have is the file. I don't have a .lib file so I'm using the LoadLibrary and GetProcessAddress functions. I followed the tutorial on the MSDN website to get the basic structure. the DLL is located in the project folder. it compiles. at run time, I am getting a numerical value for "hinstLib" so I'm assuming the DLL was found. I am getting a null value for "ProcAdd" variable. Other posters had there issues resolved by putting extern C in the DLL functions but I don't really have that option. not to mention, to my knowledge this DLL was written in plain C. I do have an interface document and am quite sure I have the function name correct (replaced with a generic example for these purposes). I honestly didn't run anything past the ProcAdd assignment because it came out NULL. Any thoughts as to why this is giving me a 0 value for the function assignment would be great appreciated. Note: unfortunately due to various reasons I can't upload the DLL.
#include <iostream>
#include "stdafx.h"
#include "Windows.h"
#include <stdio.h>
typedef int(__cdecl *MYPROC)(LPWSTR);
using namespace std;
int main()
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
hinstLib = LoadLibrary(TEXT("dllName.dll"));
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "funcName");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
//(ProcAdd) (L"Message sent to the DLL function\n");
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");
return 0;
}
Compilers usually mangle function names, then a function named funcName may appear inside the DLL with a name funcName#16 , for example... It depends on calling convention and are important for a function to be called properly. For __cdecl calling convention you probably need _funcName :-) .

Using a dll in C++ code

I want to use pjsipDll.dll in a c++ code. I got this dll from one of the sites, I only know how to build the code to obtain the dll file. So I did that and now I've the pjsipDll.dll file with me. I want to use certain functions in the DLL in my code(C++)
I tried the following code. << I haven't made/added any dll or .h file to the project, there is only the following CPP file>>
#include <iostream>
using namespace std;
int CallMyDLL(void)
{
/* get handle to dll */
HINSTANCE hGetProcIDDLL = LoadLibrary("G:\\July\\9.0\\pjsipdll\\Lib\\pjsipDll.dll");
/* get pointer to the function in the dll*/
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"dll_makeCall");
/*
Define the Function in the DLL for reuse. This is just prototyping the dll's function.
A mock of it. Use "stdcall" for maximum compatibility.
*/
typedef int (__stdcall * pICFUNC)(int, char *);
pICFUNC MyFunction;
MyFunction = pICFUNC(lpfnGetProcessID);
/* The actual call to the function contained in the dll */
int intMyReturnVal = MyFunction(5,"hello");
/* Release the Dll */
FreeLibrary(hGetProcIDDLL);
/* The return val from the dll */
returnintMyReturnVal;
}
void main()
{
cout<<"Hello World";
CallMyDLL();
getchar();
}
I learnt this way from some site, to use the function from a DLL.
The problem is, I get an ERROR:
error C2065: 'HINSTANCE' : undeclared identifier g:\july\9.0\pjproject-0.9.0\myproject\importerprojet\importerprojet\mycpp.cpp 9 importerProjet
Can anyone help me out with this. Or atleast point me to the post if this query is already addressed.
Thanks for your help,
Vinu.
You need to #include <windows.h>

Passing unmanaged C++ object into managed C++ code

So as the title states, I am looking to pass an object that is defined in an unmanaged C++ project into a managed C++/CLI project (both projects are in the same solution) while still being able to access the same methods as in the unmanaged C++ project.
This object is a class which imports functions from a separate unmanaged C++ dll (not in the same project or solution) through the means of using typedef's as shown below (excerpt of header):
#pragma unmanaged
typedef BOOL (WINAPI *someBoolFunc) (DWORD Name, DWORD Flags, TCHAR* Text);
class INeedThisClass
{
public:
INeedThisClass();
~INeedThisClass();
someBoolFunc BoolFunc;
};
extern INeedThisClass ReallyNeedThisClass;
I would really like to be able to access the externed object ReallyNeedThisClass from within my managed code by just only passing the object. By having this object I wish to access it's methods through ReallyNeedThisClass->BoolFunc. What is the best way of accessing this object from my managed code?
EDIT: I've been thinking maybe there is a way of using a property to "Get" the object from the unmanaged code and then "Set" the same object in the managed code. Could this be a proper angle to approach this issue from? I've cleaned up the question a bit to get rid of useless details.
UPDATE: I've made progress on this using a wrapper for the unmanaged C++ code, but of course there are still problems. Using the above class as an example here, I will show you what I have managed to do up until now.
Project 1: Unmanaged C++ class (example above)
Project 2: Managed C++/CLR wrapper class (example below)
Wrapper.h:
#pragma once
#include "INeedThisClass.h" //Unmanaged class INeedThisClass
using namespace System;
using namespace System::Runtime::InteropServices;
namespace INeedThisClassWrapper
{
public ref class INTC
{
public:
INTC(void);
virtual ~INTC(void);
delegate bool BoolFunc(UInt32 Name, UInt32 Flags, String^ Text);
private:
INeedThisClass *ReallyNeedThisClass;
};
}
Wrapper.cpp:
#include "Stdafx.h"
#include "Wrapper.h"
using namespace INeedThisClassWrapper
#include <msclr/marshal.h>
using namespace msclr::interop;
INTC::INTC(void)
{
ReallyNeedThisClass = new INeedThisClass();
}
INTC::~INTC(void)
{
if (ReallyNeedThisClass)
{
delete ReallyNeedThisClass;
ReallyNeedThisClass = NULL;
}
}
bool INTC::BoolFunc(UInt32 Name, UInt32 Flags, String^ Text)
{
return ReallyNeedThisClass->BoolFunc;
}
Now I am receiving errors from the compiler stating:
error C2065: 'WINAPI': undeclared identifier
error C2065: 'someBoolFunc': undeclared identifier
error C4430: missing type specifier - int assumed. Note C++ does not support default-int
Any ideas?
First, you have to export INeedThisClass from unmanaged dll and import from managed one.
//unmanaged header
#if UNMANAGED_COMPILE_DEFINITION
#define EXPORT_OR_IMPORT __declspec(dllexport)
#else
#define EXPORT_OR_IMPORT __declspec(dllimport)
#endif
class EXPORT_OR_IMPORT INeedThisClass
{
public:
INeedThisClass();
~INeedThisClass();
someBoolFunc BoolFunc;
};
Also unmanaged dll must be compiled with UNMANAGED_COMPILE_DEFINITION preprocessor definiton.
Second, remove extern INeedThisClass ReallyNeedThisClass; you don't need it. Because ReallyNeedThisClass is a member of wrapper class already.
Third, you need to marshal System::String^ to TCHAR* in order to call BoolFunc
Forth, I do not remember but if you include "Windows.h" or "Winnt.h", the error about WINAPI will be gone.