OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 04-04-2009, 12:38
Tomasz Tomasz is offline
Junior Member
 
Join Date: Aug 2006
Posts: 21
Default How to pass cdr object to c++ function as argument

I wonder how can I pass a CorelDRAW object, namely ShapeRange object, to a DLL function as an argument. I would prefer to pass the object by reference rather than by value.

In VBA I declare function:
Code:
Declare Function myFunc Lib "test.dll" Alias "_myFunc@4" (ByRef aSr As ShapeRange) As Double
and invoke it in a Sub:
Code:
Dim aSr As New ShapeRange
Dim x As Double
Set aSr=ActiveSelection.Shapes.All
x=myFunc(aSr)
MsgBox=("Pi is " & x)
In C++ DLL I have:
Code:
extern "C" double _stdcall myFunc(VARIANT& aShapeRange)
{
  double x; x=3.14; return x;
}
Of course the function is declared earlier in the code.
So far so good - no errors are generated and Message Box shows "Pi is 3.14". Thus the aShapeRange object must have been accepted by the DLL. But how can I access any member of the aShapeRange object within the function?

Last edited by Tomasz; 04-04-2009 at 17:15.
Reply With Quote
  #2  
Old 05-04-2009, 14:37
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default

First of all, you can't use references in VBA, so can't do VARIANT&

If you declare VARIANT, then you should do so in your VBA declaration as well:

Code:
Declare Function myFunc Lib "test.dll" Alias "_myFunc@4" (ByVal aSr As Variant) As Double
in C++:

Code:
extern "C" double _stdcall myFunc(VARIANT aShapeRange)
{
  double x; x=3.14; return x;
}
But I'd do this instead:

Code:
Declare Function myFunc Lib "test.dll" Alias "_myFunc@4" (ByVal aSr As Object) As Double
in C++:
Code:
extern "C" double _stdcall myFunc(IDispatch* aShapeRange)
{
  double x; x=3.14; return x;
}

This way once you've got IDispatch, you can use IDispatch::QueryInterface to get the IDrawShapeRange (or IVGShapreRange) interface:

Code:
extern "C" double _stdcall myFunc(IDispatch* aShapeRange)
{
  double dResult = 0.0;
  IDrawShapeRange* pIShapeRange = NULL;
  HRESULT hr = aShapeRange->QueryInteface(__uuidof(IDrawShapeRange), &pIShapeRange);
  if(SUCCEEDED(hr))
  {
    long nCount = 0;
    hr = pIShapeRange->get_Count(&nCount);
    if(SUCCEEDED(hr))
    {
      dResult = static_cast<double>(nCount);
    }
    pIShapeRange->Release();
  }
  return dResult;
}
This should return the number of shapes in the shape range back to your VBA code.
Reply With Quote
  #3  
Old 08-04-2009, 16:59
Tomasz Tomasz is offline
Junior Member
 
Join Date: Aug 2006
Posts: 21
Default

Alex,

thank you very much for directing me at IDispatch. It should resolve my problem. Hovewer my VC++ builder refuses to build the code you have suggested. The problem is within:

Code:
HRESULT hr = aShapeRange->QueryInterface(__uuidof(IDrawShapeRange), &pIShapeRange);
The error message I receive is:

error C2664: 'HRESULT IUnknown::QueryInterface(const IID &,void **)' : cannot convert parameter 2 from 'CorelDRAW::IDrawShapeRange **' to 'void **' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast.

How it is possible at all??
__uuidof(IDrawShapeRange) refers to IDrawShapeRange Struct GUID in coreldraw.tlh. At lease code definition window in VC++ enviroment properly identifies IDrawShapeRange struct when __uuidof(IDrawShapeRange) is highlighted.
&pIShapeRange is a variable refered to by the pointer of the same IDrawShapeRange type as above! So how these types can be unrelated??

PS. Due to constant problem with my CDR X4 vs. VC++ 2008 installation I have tested this on the machine with CGS 12 and VC++ 2008 EE.

Last edited by Tomasz; 08-04-2009 at 17:01.
Reply With Quote
  #4  
Old 09-04-2009, 22:36
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default

That is correct, my mistake... The code should read:

Code:
HRESULT hr = aShapeRange->QueryInterface(__uuidof(IDrawShapeRange), (void**)&pIShapeRange);
Reply With Quote
  #5  
Old 11-04-2009, 09:49
Tomasz Tomasz is offline
Junior Member
 
Join Date: Aug 2006
Posts: 21
Default

Alex,

thank you very much! You are great! Now it works flawlessly.
I only wonder why (void**) declaration? After all pIShapeRange pointer has been already declared as IDrawShapeRange*.
I must learn more about IDispatch method... It is essential when one wants to make expansions for third party software.

Tomasz
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem pass argument to COM MarcinK CorelDRAW/Corel DESIGNER VBA 0 04-12-2008 11:05
start PAINT12 with argument ("/NOSPLASH" no effect!) lebaron Corel Photo-Paint VBA 1 13-08-2008 04:57
function delete after duplicate dominiqueL CorelDRAW/Corel DESIGNER VBA 2 18-10-2003 01:35
Function names Lev Oberon Function Plotter 1 29-08-2003 12:04
How can I get data from function? Elie CorelDRAW/Corel DESIGNER VBA 3 15-05-2003 06:08


All times are GMT -5. The time now is 07:52.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com