I am currently attempting to integrate the AzureStorageCPP into Unreal Engine 4 by using this documentation page. What I am doing is to create a custom C++ DLL wrapper around AzureStorageCPP, and then have Unreal Link this DLL in Unreal Code.
I installed the AzureStorageCPP as DLLs, via vcpkg. On completion, wastorage.dll is one of the important DLLs that needs to be linked.
The C++ DLL wrapper I wrote around AzureStorageCPP is below:
#include "was/storage_account.h"
#include "was/blob.h"
void UploadFileToAzure(char* FileName)
{
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(U("DefaultEndpointsProtocol=https;AccountName=test;AccountKey=test"));
}
As far as I can tell, azure::storage::cloud_storage_account::parse lives in wastorage.dll
The issue is that when this parsing code is executed as a C++ Console, it runs just fine. The ConnectionString is passed correctly down the callstack to wastorage.dll, and the connection string is not garbage.
Here is the view one level down the callstack from the code above.
However, if I put the code into the C++ wrapper DLL I made called AzureStorage.DLL, link it into Unreal, and call from there, with the same call stack the string becomes garbage when it gets passed into wastorage.dll to parse.
I thought this might be related to this issue, but I still don't understand how to fix this, as I don't control the Parse method that is from AzureStorage.
Here is how I created the C++ DLL wrapper header, the source is already shown above:
AzureStorage.h
#pragma once
#define AZURESTORAGEDLL_API _declspec(dllexport)
#ifdef __cplusplus //if C++ is used convert it to C to prevent C++'s name mangling of method names
extern "C"
{
#endif
void AZURESTORAGEDLL_API UploadFileToAzure(char* FileName);
#ifdef __cplusplus
}
#endif
Related
I have an app that uses a modified version of Lua 5.2. Lua is embedded in an exe and as far I can see (with Dependency Walker) there are no export symbols for its Lua functions. I have no sources of this app. I need to write C module for Lua to use with this app.
I successfully require it in the Lua file that this app processes, but as soon as I'm trying to call any lua_* function app crashes with Access violation error.
Is there a way to find addresses/names of embedded Lua functions and call them?
I asked app developers if I'm allowed to do this, they don't seem to care. The only thing is that modification of the app itself is prohibited.
UPD: As far as I can see, I get Access violation because I'm trying to use Lua proxy lib and it fails to pass calls to exe Lua C functions.
If I'm not using Proxy DLL, I get Multiple VMs detected. Any way to solve this?
My Lua C module code:
#if defined(WIN32) || defined(_WIN32)
# define LUA_BUILD_AS_DLL /* for #define LUA_API __declspec(dllimport) */
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include <lua.h>
#include <lauxlib.h>
#ifdef __cplusplus
}
#endif
#if defined(WIN32) || defined(_WIN32)
# ifdef __cplusplus
# define MY_LUA_API extern "C" __declspec(dllexport)
# else
# define MY_LUA_API __declspec(dllexport)
# endif
#else
# define MY_LUA_API extern "C"
#endif
using namespace std;
static int Lmytest_test(lua_State *L) {
// Check the arguments.
int argn = lua_gettop(L);
if (argn != 1 || !lua_isstring(L, 1)) {
lua_pushstring(L, "Give me one string argument please!");
lua_error(L);
}
lua_pushstring(L, "Hello world!");
return 2;
}
static const struct luaL_Reg lib[] = {
{ "test", Lmytest_test },
{ NULL, NULL }
};
MY_LUA_API int luaopen_mytest(lua_State* L) {
printf("Before newlib\n");
#if LUA_VERSION_NUM >= 502
luaL_newlib(L, lib);
#else
lua_createtable(L, 0, sizeof(lib) / sizeof(lib[0]));
luaL_register(L, NULL, lib);
#endif
printf("After newlib\n");
return 1;
}
For those who ask what is Lua proxy lib:
http://lua-users.org/wiki/LuaProxyDll
http://lua-users.org/wiki/LuaProxyDllTwo
http://lua-users.org/wiki/LuaProxyDllThree
http://lua-users.org/wiki/LuaProxyDllFour
I need to write C module for Lua to use with this app.
Well... you can't.
If their program does not export Lua's DLL interface, and it doesn't load Lua's DLL, then that means it statically linked with Lua. Unless you actually hack the executable, your DLL module will be talking to a Lua implementation that the main executable is not using. That's why you got a runtime crash; two different pieces of code talking to the same memory doesn't work out.
By statically linking to Lua, the developers of the application made it essentially impossible for users to write their own C modules for their uses of Lua without hacking the binary.
Is there a way to find addresses/names of embedded Lua functions and call them?
Not without a decompiler or really good assembly skills. And thanks to link-time optimization, even those may fail you, since some Lua APIs may have been inlined.
You most certainly can write a C dll and if the Lua environment is a properly compatible Lua system, then you should be able to achieve what you are looking to do. There are a number of things you need to check though:
Find out exactly what Lua version is running in the system you use.
This will let you determine what type of library you will need to compile against.
print( _VERSION )
Check other dll modules this Lua environment is using. Use dependency walker or similar. They should be using the luaopen_() type entry point. If this is the case, then your entry point above should be ok.
Check what you are passing to your methods. You cannot pass lightdata (pointers) between the Lua and your dll - mainly because their allocations will be in a different space. You will get access violations from this.
Check for any external libraries that the Lua system may be using. It is common that you will find a lua5.1.dll or similar. You need to be linking with this to ensure the methods you use, will be within the same application process space.
Often problems like this are more likely that the Lua implementation has been modified, and Lua bytecode and interfaces are not consistent, so very difficult to build against.
Hope this helps.
Btw. Use dependency walker to look into exe's and dll's for available methods. Lua uses the stdcall name mangling, so they are always visible.
http://www.dependencywalker.com/
Searching for JNI, I've always found something like:
Method in C/C++, call in Java
Method in Java, call in C/C++
But never method in C/C++, call in C/C++ using JNI.
I ask that, because I have a 3rd party Java library, which has some C/C++ libraries, and I've always used Java to call them. And I would like now, to create some program using C instead of Java. Unfortunately they don't provide the any API in C/C++, only in Java.
Is it possible, if yes how can I try to do it?
Thank you in advance!
The JNI methods are always publicly exposed with an extern C signature.
This means that you could just link to the JNI dll directly if you have the method signature. (which you can easily get if you look at the Java class native method signature).
But you will have to make sure you give the method the environment it expects.
This means pass the "this" object and environment object.
You'll have to make sure that the class loader object has the right packages loaded in it. In most cases JNI code assumes the environment that called the method has the right class loader in it already (because the java method was called...).
And there's also an issue with local references that JNI code might assume are released automatically from the stack (which doesn't happen in C++ native threads).
Anyway, it would look like this:
//MyClass.java
class MyClass
{
native void Blah();
}
//method signature, some header file:
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_MyClass_Blah
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
//Call the function, C++ code
Java_MyClass_Blah(myEnv, myClassInstanve);
I Currently have a C win32 application in place, I am wanting to make a GUI to communicate and call various api command in my application. In visual studio I select project -> add new items -> [C++ -> windows form] and a nice form pops up. My question is how do I make calls from the form.h file produced. A nice example to see would be seeing a gui modifying a value and that value simply printing out in the console (a function in the application external from the form.h file). If not, any links related would be nice
Thanks!
Define header file for your C library with C++ guards.
#ifdef __cplusplus
extern "C" {
#endif
int my_c_function1(void);
int my_c_function2(void);
struct my_c_struct { ... };
void my_c_function3(struct my_c_struct *p);
...
#ifdef __cplusplus
}
#endif
It is important that all type and function declarations are between #ifdef blocks.
Those blocks tell C++ compiler that the data types and functions from that scope belong to C language and require C ABI bindings.
So you would be able to use those types and functions without error in both C and C++ source files.
I am trying to use digital scope Vellman PCSU1000 in one of my projects which I am writing in C++ in Visual Studio 2010.
In order to do this I need to use two DLLs provided by the producer of the device: DSOLink.dll and PCSU1000D.dll. I do not have any other associated files. As far as I understood from the manual those DLLs were writen and compiled in Deplhi. There is a short description of the functions contained in each of DLL and how to use them in different programming languages. For C++ language producer chose Borland C++ Builder and added following code (for using DSOLink.dll, it is analogous for PCSU1000D.dll):
DSOLink.h
//---------------------------------------------------------------------------
// DSOLink.h
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif
#define FUNCTION __declspec(dllimport)
FUNCTION bool __stdcall DataReady(); /*this particular line is added by me based on
list of the functions listed by dumpbin/exports DSOlink.dll and manual*/
FUNCTION __stdcall ReadCh1(int* ptr);
FUNCTION __stdcall ReadCh2(int* ptr);
#ifdef __cplusplus
}
#endif
//---------------------------------------------------------------------------
According to instruction found in MSDN: http://support.microsoft.com/kb/131313
I have created a .lib file. Thanks to that the Build Solution finishes succesfully but Debugging returns error message "Cannot find the entry point of function _DataReady#0 in DSOLink.dll".
From the article: http://bcbjournal.org/articles/vol4/0012/Using_Visual_C_DLLs_with_CBuilder.htm?PHPSESSID=ab5f6c83b3da921591a490a5accb5bf7 I know that in C++ Builder the keyword *__stdcall* indicates no decoration convetion while in VS means exactly opposite.
I understand that in this situation I am forced to use *__stdcall* in VS (it has to be the same method of managing stack cleaning in DLL and in header used in aplication) and no-decoration method at the same time...I have read that .DEF files may help but I am lost how exactly it should be used. I was trying to create DEF in different ways:
EXPORTS
DataRead=DataRead
.
.
.
EXPORTS
DataRead=_DataRead#0
.
.
.
EXPORTS
_DataRead#0=DataRead
.
.
.
and added one of them at once to my project but none of them solves the problem.
Does anybody have an idea what is the proper way of forcing VS not to use and not to expect decorated names with *__stdcall*?
Or maybe reason of my problem is something different and somebody knows it?
Thanks in advance.
I have a (big and complex) C++ program with its own classes and methods.(obviously)
I would like to load at runtime a dll. From the main program i want to call a function inside the dll passing a class instance , and this function will use the method of this class.
In pseudo code
Main program:
class MyClass{
myattr1();
myattr2();
mymethod1();
mymethod2();
}
void main(){
MyClass* object = &(new MyClass())
handle_of_the_dll = *some function that load the dll*
dllfunc = getfunc(handle_of_the_dll, "interesting_function")
dllfunc(object)
[...etc...]
and the dll
#ifdef __cplusplus
extern C {
#endif
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
#include all the needed headers from main program, where the class is defined
EXPORT void interesting_functionn(MyClass object){
object.mymethod1();
}
#ifdef __cplusplus
}
#endif
Is this valid/doable/right?
EDIT:
i know that this would be a poor design for a general program, but this method is intended to give the users the ability to use their custom dll that can access the api of the main program. Something like Cpython modules
It is valid and doable. Generate a sample DLL in visual C++ and the code will have all the bases covered. Replace the static linking in the client with LoadLibrary and GetProcAdress.
Name mangling could be an issue, DependencyWalker will help you there.
Keep a low profile with emory management. For example, if you use a memory manager on one side but not on the other, your begging for trouble.
I'd say it's possible, but with all due respect, it looks like bad design to me. There is a circular dependency between the main program and the dll: the program calls functions from the dll, and viceversa. In an ideal design, the main program would consume some dll functions, and the dll wouldn't know anything about the main program, or in general, about its clients.
Perhaps you should isolate the functions from the main program that the dll needs to use in a common dll or static lib which both the program and dll use.
As long as the methods are declared virtual and you pass the object by reference or pointer there should be no problems. If however you want to access non-virtual methods of the class you will need to put it's implementation in a separate DLL.