在Sketchup Ruby中调用dll库时出现Ruby Sketchup错误

时间:2014-02-24 04:19:40

标签: ruby dll dllimport dllexport sketchup

我希望Sketchup可以导入多个文件。但遗憾的是,Sketchup API并不支持它。所以我创建了一个dll库。它调用Window API函数:GetOpenFileName用于显示多选对话框,获取所选文件名并以格式化字符串显示它们(选择目录| filename1 | filename2 ...)。然后我把它和Win32API.so放到Sketchup的Plugin文件夹中。但是当我打开Sketchup时,它显示错误:

加载文件时出错GetProcAddress:ShowDialog或ShowDialogA(ShowDialog是我的dll的一个函数)

这是我在dllmain.cpp(C ++代码)中的代码

#define EXPORT _declspec(dllexport)
HWND hDlg = NULL;
wchar_t* buffer = new wchar_t[32768];

EXPORT void ShowDialog(void)
{
    OPENFILENAME ofn;
    wchar_t szFile[32768];
    // Initialize OPENFILENAME
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hDlg;
    ofn.lpstrFile = (wchar_t*)szFile;
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags =  OFN_ALLOWMULTISELECT | OFN_ENABLESIZING | OFN_EXPLORER;
    if(GetOpenFileName(&ofn) == TRUE)
    {
        if(szFile[ofn.nFileOffset-1] != '\0')
        {
            wcscpy_s(buffer,sizeof(szFile),szFile);
        }
        else
        {
            //Multi-select
            wchar_t* p= szFile;
            int i =0;
            while(*p)
            {
                wchar_t* temp = new wchar_t[1024];
                //Copy p into temp
                wcscpy_s(temp,1024,p);
            if(i==0)
            {
                wcscpy_s(buffer,1024,temp);
            }
            else
            {
                wcscat_s(buffer,1024,temp);
            }
            wcscat_s(buffer, sizeof(buffer) * wcslen(buffer), L"|");
            i++;
            p += lstrlen(p) + 1;
        }
    }
    int len = wcslen(buffer);
    buffer[len]='\0';
}



EXPORT void GetResult(char* pString)
{
    int len = wcslen(buffer);
    for(int i =0 ; i < wcslen(buffer);i++)
    {
        pString[i] = (char)buffer[i];
    }
    pString[len]='\0';
}

这是我在ruby文件中的脚本。

require 'sketchup.rb'
require "Win32API"
path = Sketchup.find_support_file("\Plugins\\")
#my dll has name MultiUploadProject.dll
mydll = path + "\\MultiUploadProject.dll"
if (!FileTest.exist?( mydll ) )
    UI.messagebox(sprintf("MultiUploadProject.dll not found in SketchUp plugins folder: %s\n",mydll))
end
ShowDialog = Win32API.new(mydll,"ShowDialog",["V"],"V")
GetResult = Win32API.new(mydll,"GetResult",["P"],"V")
def load_dialog
    ShowDialog.call
    string = " " * 32768
    GetResult.call(string)
end

我使用Visual Studio 2012构建dll,而我的Sketchup是Sketchup 8

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:2)

导出需要是C导出(没有C ++名称修改)。尝试像这样定义EXPORT宏:

#define EXPORT extern "C" _declspec(dllexport)

或在extern“C”

中定义导出的函数
extern "C" {

// Exported functions here.

}