On 28/11/2014 at 10:09, xxxxxxxx wrote:
User Information:
Cinema 4D Version: 13
Platform: Windows ;
Language(s) : C++ ;
---------
Hi,
I can't get .dll files to load using LoadLibrary().
When C4D starts up a window pops up saying it can't find the .dll file.
It works fine if I put the .dll file in the main Maxon folder. But I want to store & load the .dlls in my plugin's folder structure.
I also tried putting the .dll file here: C:\Program Files\MAXON\my C4D version\resource\libs\win64\SimpleDLL.dll
But C4D still doesn't find it when it launches.
This is how I'm trying to load the .dll from my C: drive. But it's doesn't work.
What am I doing wrong?
And do I really need to do all of this type conversion gymnastics just to load the .dll file with LoadLibrary()?
#include "c4d.h"
#include <string.h>
#include "c4d_basedocument.h"
#include <string> //for std::wstring
#define UNICODE //for LPCTSTR
#include <Windows.h>
using namespace std;
//This custom method converts a Cineam4D string type to a STD C++ string type
std::string C4DStringToStdString(const String& s)
{
std::string r;
const LONG len = s.GetCStringLen(STRINGENCODING_XBIT);
CHAR* tmp = new CHAR[len + 1];
if (tmp)
{
if (len == s.GetCString(tmp, len + 1, STRINGENCODING_XBIT))
{
r.assign(tmp);
}
}
delete[] tmp;
return r;
}
//This custom method converts a string to a wstring type so we can use it in FindWindow() later on
std::wstring string2wstring(const std::string &s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
Bool RegisterSimplePluginCMD(void);
Bool PluginStart(void)
{
if (!RegisterSimplePluginCMD()) return false;
return true;
}
void PluginEnd(void)
{
}
Bool PluginMessage(Int32 id, void* data)
{
switch (id)
{
case C4DPL_PROGRAM_STARTED:
{
//Get the path to the.dll file on your HD
Filename fn = "C:\\SimpleDLL.dll";
GePrint(fn.GetFileString()); //<---just checking to see if the dll was found
//Convert the C4D String type into an LPCWSTR type
String c4dPath = fn.GetString(); //The C4D String type path to the .dll file
string stlPath = C4DStringToStdString(c4dPath); //Convert to a STL type string
wstring buffer = string2wstring(stlPath); //A temporary buffer is required
LPCWSTR path = buffer.c_str(); //Converted to an LPCWSTR type
//Load the .dll file so C4D can use it
HMODULE WINAPI dll_handle = LoadLibrary(path);
//or
//HINSTANCE dll_handle = LoadLibrary(path);
}break;
case C4DPL_INIT_SYS:
{
if (!resource.Init())
return false; don't start plugin without resource
return true;
}
case C4DMSG_PRIORITY:
//react to this message to set a plugin priority (to determine in which order plugins are initialized or loaded
//SetPluginPriority(data, mypriority);
return true;
case C4DPL_BUILDMENU:
//react to this message to dynamically enhance the menu
//EnhanceMainMenu();
break;
}
return false;
}
-ScottA