Reputation: 3751
This is my first time working with DLL's and I am at a bit of a loss.
Not because I don't understand the code. But because all of the tutorials Im following and they are breaking at some points.
First I attempted this, but my work was cut short when the javah
command would not work erroring out with the message: Error: Could not find or load main class com.sun.tools.javah.Main
I then moved on to make my own ddl's so that I can call them from C a library. Found this video and I was able to follow it and its page on the Microsoft page to make the dll. Note that I am fully capable of following the example. The one part that I struggle with is what happens if I don't have the header file or a lib file to the DLL. So then I started following this example and visual studios is saying that import my not exist.
I found that others were able to get the same exact thing working.
What am I doing wrong? End goal, I'd like to know how to create a DLL file like in the video. And only with the DLL file in my possession, access its functions.
So if the DLL was created with the following:
Header
namespace nmspace
{
class myclass{
public:
static __declspec(dllexport) void Crap();
};
}
Source.cpp
#include "Header.h"
using namespace std;
#include <iostream>
namespace nmspace
{
void myclass::Crap(){
cout << "Some Crap";
}
}
How would I call it via LoadLibarary or LoadLibararyA. Note that this did not work for me
To read the dll:
#include <Windows.h>
#include <iostream>
using namespace std;
void PrintFullPath(char * partialPath)
{
char full[_MAX_PATH];
if (_fullpath(full, partialPath, _MAX_PATH) != NULL)
printf("Full path is: %s\n", full);
else
printf("Invalid path\n");
}
int main(){
HMODULE hMod = LoadLibrary("SimpleDLL.dll");
if (NULL == hMod)
{
cout << "LoadLibrary failed\n";
PrintFullPath(".\\");
system("PAUSE");
return 1;
}
}
In the above code, I print out the current working directory. In that directory I have placed my dll. Still the dll is not being loaded.
I am using Visual Studios if that matter. I would also be intrested to see how I would compile the above c++ code via command line and include the dll with it!
EDIT:
I also found this but it relys on the header file as well. Note that I will know what the function names and formats are through the documentation. I just have no header file!
Upvotes: 1
Views: 1523
Reputation: 7911
DLLs created with C# are not like other DLLs. They are technically called .NET assemblies. They rely on the Common Language Runtime (CLR) in the same way that Java bytecode relies on the Java Virtual Machine. The video you posted a link to is not creating a .NET assembly, but rather a native Windows DLL.
Obviously, if you have two virtual machines loaded in a process, things are going to get complicated. They both have their own ideas about how to use memory, garbage collection, layout of objects in memory, threading and so on.
That's not to say that what you are trying to do is impossible, but it's a lot more complicated than loading native libraries with LoadLibrary
.
You might like to take a look at the following projects to help you out:
Upvotes: 6