简体   繁体   English

从Excel VBA调用的C ++ DLL仅在Visual Studio中的调试实例中有效

[英]C++ DLL called from Excel VBA only works from debug instance in Visual Studio

I have created a 64 bit dll in C++. 我在C ++中创建了一个64位dll。 There is an Excel macro that can call it by listing the full path to the dll. 有一个Excel宏可以通过列出dll的完整路径来调用它。 However, it only works if I open the containing Excel file by right clicking on the C++ project and selecting Debug->Start New Instance. 但是,仅当我通过右键单击C ++项目并选择“调试”->“启动新实例”打开包含Excel文件时,此方法才有效。 If I open the Excel file directly from the file explorer and try to run the dll, it gives me a "File not found" message box that it can't find the dll, with the path listed. 如果我直接从文件资源管理器中打开Excel文件并尝试运行dll,它会给我一个“找不到文件”消息框,提示它找不到dll,并列出了路径。 It gives the same error in Release mode. 在发布模式下,它会给出相同的错误。 However, when I check the path, the dll is present and seems to compile fine to the specified location. 但是,当我检查路径时,dll存在并且似乎可以正确编译到指定位置。

This hasn't happened with other dlls I have made. 我制作的其他dll并没有发生这种情况。 A similar dll can be called whether I open it from the file explorer or a debug instance. 无论是从文件资源管理器还是调试实例打开它,都可以调用类似的dll。 There isn't any apparent reason for the difference. 差异没有明显的原因。 They both call other dlls, have namespaces, and have classes, but the problem seems to be occurring before it even reaches the code. 它们都调用其他dll,具有名称空间和类,但是问题似乎在甚至到达代码之前就已经发生。

C++ (test function in the DLL): C ++(DLL中的测试功能):

int test(int &x)
{
    return x+8;
}

VBA code: VBA代码:

Private Declare PtrSafe Function test Lib _
"[path to dll]" (ByRef x As Long) As Long

'  Use function on worksheet
 Function testThis(x As Long) As Long
    On Error GoTo Catch
    testThis = test(x)
Catch:
    If Err <> 0 Then
        MsgBox (Err.Description)
    End If
    End Function

If I enter "2", for instance, it should return "10" in all cases. 例如,如果我输入“ 2”,则在所有情况下都应返回“ 10”。 This happens from the debug Excel instance started through Visual Studio only, but gives me an error message that it can't find the DLL when the file is opened directly. 这是通过仅通过Visual Studio启动的调试Excel实例发生的,但给我一个错误消息,当直接打开文件时找不到DLL。 I have no idea why. 我不知道为什么。

Edit: The path is correct in VBA. 编辑:路径在VBA中是正确的。 Do you honestly think I haven't checked that? 老实说,您是否还没有检查过? It's private info and if it were wrong it wouldn't work in the debug Excel instance. 这是私人信息,如果有误,它将无法在调试Excel实例中运行。

Dependency Walker gives the following errors: Error: At least one required implicit or forwarded dependency was not found. 依赖项Walker出现以下错误:错误:找不到至少一个必需的隐式或转发依赖项。 Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module. 错误:由于隐式相关模块中缺少导出功能,因此至少一个模块的导入无法解析。 Error: Modules with different CPU types were found. 错误:找到了具有不同CPU类型的模块。 Error: A circular dependency was detected. 错误:检测到循环依赖性。 Warning: At least one delay-load dependency module was not found. 警告:至少找不到一个延迟负载依赖性模块。 Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module. 警告:由于依赖于延迟负载的模块缺少导出功能,因此至少一个模块的导入无法解析。

I'm looking into what these could mean, but haven't used this before and am a C++ and VBA novice. 我正在研究这些可能意味着什么,但是以前没有使用过,并且是C ++和VBA的新手。

I couldn't identify an issue with dependencies, but Excel does need some help finding them in general. 我无法确定依赖项是否存在问题,但是一般来说Excel确实需要一些帮助。 In order to get any dependent DLLs to work at all when called from Excel, including this case, here is one way to do so. 为了使从Excel调用时所有依赖的DLL都能正常工作,包括这种情况,这是一种方法。 Let's say the dependent DLL I want to call directly is "A" and an installed one it depends on is DLL "B". 假设我要直接调用的从属DLL是“ A”,而它依赖的已安装DLL是“ B”。 In order to call any function from the dependent DLL A, I first have to call any function from the independent DLL B. It doesn't matter which function or even if the function I'm calling from Excel directly depends on the DLL B. Using this hack, I can get all other functions in A to run, provided there are no unrelated issues. 为了从依赖的DLL A调用任何函数,我首先必须从独立的DLL B调用任何函数。无论哪个函数,甚至我从Excel调用的函数直接取决于DLL B都没有关系。使用此技巧,只要没有不相关的问题,我就可以运行A中的所有其他功能。 If I open the Excel file directly from the File Explorer and try to run the DLL without this hack, it gives me assorted errors like VALUE or "cannot find the dll". 如果我直接从文件资源管理器中打开Excel文件,并尝试在没有这种黑客的情况下运行DLL,它将给我带来诸如VALUE或“找不到dll”之类的各种错误。

I also cleaned up the dependencies in the projects Properties->Input->Additional dependencies, to something like B.lib;%(AdditionalDependencies), but this didn't have an effect. 我还清理了项目Properties-> Input-> Additional依赖项中的依赖项,类似于B.lib;%(AdditionalDependencies),但这没有任何效果。 It just looks better. 看起来好多了。

I haven't been able to get LoadLibrary to work yet, but that was suggested in the comments. 我还无法使LoadLibrary正常工作,但是在注释中建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM