Pass a managed C++ pointer from VBA to a C++ DLL

  • Thread starter Thread starter shiftingspanner
  • Start date Start date
S

shiftingspanner

Guest
Im not a programmer, just an enthusiastic hacker. I’m trying to write a centralised DLL that I can access from both the VBA environment within Visio as well as from another C++ development. I’m trying to get the first bit to work. What I want to do is to be able to pass a Visio shape to the DLL where I can process it and return it. I’ve written the following C++ development:

Configuration Type Dynamic Library (.dll)

Use of MFC Use Standard Windows Libraries

Use of ATL Not Using ATL

Character Set Use Unicode Character Set

Common Language Runtime support Common Language Runtime Support (/clr)

Whole Program Optimisation No Whole Program Optimization


The DLLver8.cpp file looks like this:

// DLLver8.cpp
//This is the main DLL file.
#include "stdafx.h"
#include "DLLver8.h"
double __clrcall AddDouble(double a, double b){
return a+b;
}
bool __clrcall AddText(Visio::Shape^ InputShape){
try{
InputShape->Text = "DLL Return";
return true;
}
catch(...){
return false;
}
}

The DLLver8.h file looks like this:

// DLLver8.h
#pragma once
using namespace System;
double __clrcall AddDouble(double a, double b);
bool __clrcall AddText(Visio::Shape^ InputShape);

The DLLver8.def file looks like this:

//DLLver8.def
LIBRARY "DLLver8"
EXPORTS
AddDouble
AddText

The VBA code looks like this in a module:

Option Base 0
Option Explicit
Public Declare Function AddDouble Lib "C:\Users\nortj0\Documents\Visual Studio 2008\Projects\DLLver8\Debug\DLLver8.dll" (ByVal a As Double, ByVal b As Double) As Double
Public Declare Function AddText Lib "C:\Users\nortj0\Documents\Visual Studio 2008\Projects\DLLver8\Debug\DLLver8.dll" (ByRef InputShape As Visio.Shape) As Boolean

And this in the ThisDocument

Sub Object(VisioShape As Visio.Shape)
Dim Test As Double
Dim TestBool As Boolean
Test = AddDouble(5.1, 6.2)
VisioShape.Text = "Pre DLL"
TestBool = AddText(VisioShape) Crash and burn here!!!
End Sub

The problem I have is that when I run the VBA code the code crashes to the extent that Visio shuts down. It fails at the “TestBool = AddText(VisioShape)” statement in the VBA code.

If I remove the references to the Visio Shape in both the DLL and in the VBA then it works fine, just returning a Boolean true or false. The AddDouble works just fine too, so I’m hurting something somewhere with this Visio.Shape.

Can anyone help me with how to pass a Visual C++ pointer (^) reference from VBA to C++?

Continue reading...
 
Back
Top