COM SAFEARRAY从C ++到VBA的内存

时间:2015-12-09 13:32:29

标签: c++ vba com

我想在UDT中创建一个用户定义类型(UDT)的SAFEARRAY。 我有一个包含S结构的结构数据。 S由S1和S2组成。

我需要使用COM函数在c ++中创建我的Data对象的数组S.

当我从VBA调用我的函数来创建数组时,VBA在函数结束时崩溃。 如果我从S中删除s7,它就能完美运行。

我将结构与4个字节对齐。

我在网上找不到任何有关此问题的帮助。

您可以在此处查看代码:

  • VBA代码:

    start 'myproject\bin\myserver.bat' myserver.yml
    start 'myproject\bin\myworker.bat' myworker.yml
    cd ui
    java -jar "target\myui.jar"
    
  • C ++代码:

  • idl文件:

    #If VBA7 Then
        Private Declare PtrSafe Function Cpp_Test2 Lib "MYLIB.dll" (i As Data) As Long
    #Else
        Private Declare Function Cpp_Test2 Lib "MYLIB.dll" (i As Data) As Long
    #End If
    
    Public Type S1
        d1 As Double
        S1 As String
        S2 As String
        d2 As Double
        d3 As Double
        i1 As Long
        i2 As Long
        d4 As Double
        d5 As Double
        s3 As String
        s4 As String
        s5 As String
        s6 As String
        d6 As Double
        d7 As Double
        d8 As Double
        s7 As String
    End Type
    
    Type S2
        n As Long
        x() As Double
        y() As Double
    End Type
    
    Type S
        ms1 As S1
        ms2 As S2
    End Type
    
    Type Data
        d As Double
        n As Long
        ms() As S
    End Type
    
    Sub TestFunction()
        Dim d As Data
        MsgBox Cpp_Test2(d)
        Stop
    End Sub
    
  • cpp文件:

        typedef [uuid(C8166034-DF8D-460A-B38E-F9342694AABA)] struct S1
        {
            double d1;
            BSTR s1;
            BSTR s2;
            double d2;
            double d3;
            int i1;
            int i2;
            double d4;
            double d5;
            BSTR s3; 
            BSTR s4; 
            BSTR s5; 
            BSTR s6;
            double d6;
            double d7;
            double d8;
            BSTR s7;
        } S1;
    
        typedef [uuid(539CF724-616E-46B6-8990-AD14F8190737)] struct S2
        {
            int n;
            SAFEARRAY(double) x; //dbl
            SAFEARRAY(double) y; //dbl
        } S2;
    
        typedef [uuid(0B689485-F347-4414-BF86-07926AC2526C)] struct S
        {
            S1 s1;
            S2 s2;
        } S;
    

我不明白为什么它不起作用。 我已经没有问题地使用过SafeArrayCreateEx,但这里似乎存在内存问题。

0 个答案:

没有答案