[Solved] Can macros using a dll written in c#

Discuss the spreadsheet application

[Solved] Can macros using a dll written in c#

Postby samchanfs » Wed Aug 03, 2011 7:41 am

i'm trying to use dll written in c# but, i don't know how to do it .
can anyone help me?
thx
Last edited by samchanfs on Fri Aug 05, 2011 6:06 am, edited 1 time in total.
OpenOffice 3.3 on Windows XP
samchanfs
 
Posts: 14
Joined: Wed Aug 03, 2011 7:33 am

Re: Can macros using a dll written in c#

Postby Charlie Young » Wed Aug 03, 2011 8:56 am

samchanfs wrote:i'm trying to use dll written in c# but, i don't know how to do it .
can anyone help me?
thx


This could turn into a rather involved thread which would belong in the macros forum. But for starters, let's discuss calling dll functions in a OOo Basic macro.

That uses the Declare statement (see Basic help)

Code: Select all   Expand viewCollapse view
Declare {Sub | Function} Name Lib "Libname" [Alias "Aliasname"] [Parameter] [As Type]


The name is what you want to use in Basic, the "Aliasname" is the exported function name in the dll. I don't know much about c#, but I'll give a c++ example, the source code looks like

Code: Select all   Expand viewCollapse view
__declspec(dllexport) double add2numbers(double a, double b)
{
   return a + b;
}


After compiling the dll, it will contain the exported name, probably mangled, something like

Code: Select all   Expand viewCollapse view
?add2numbers@@YANNN@Z


At least that's what I wind up with using __declspec(dllexport), though it might also have been created with a def file.

How to find the exported names? In c++ there is a thing called DumpBin, but you can also just load the dll into a text or hex editor and look for names such as above.

Code: Select all   Expand viewCollapse view
Declare Function addnumbers Lib "path\filename.dll" Alias "?add2numbers@@YANNN@Z" (ByVal a As Double,ByVal b As Double) As Double
Function Add(a As double,b As Double) As Double
   Add = addnumbers(a,b)
End Function


There are more considerations as to when to use ByRef and ByVal in the argument list, what to do with classes, structures, and pointers, etc.

Hopefully this'll get you started.

Note well that Declare is used at the module level, not within a Sub or Function.
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Can macros using a dll written in c#

Postby samchanfs » Wed Aug 03, 2011 10:14 am

I can't find the Aliasname in C# dll and when i use the function it have an error at this code

Declare Function myFirstDll Lib "c:/testdll.dll" Alias "myFirstDll" (ByVal a as string) As String

it was show me an error "basic runtime error sub-procedure or function procedure not defined"
OpenOffice 3.3 on Windows XP
samchanfs
 
Posts: 14
Joined: Wed Aug 03, 2011 7:33 am

Re: Can macros using a dll written in c#

Postby Charlie Young » Wed Aug 03, 2011 6:37 pm

samchanfs wrote:I can't find the Aliasname in C# dll and when i use the function it have an error at this code

Declare Function myFirstDll Lib "c:/testdll.dll" Alias "myFirstDll" (ByVal a as string) As String

it was show me an error "basic runtime error sub-procedure or function procedure not defined"


Where did the dll come from? If you wrote it yourself you can control the aliasnames. There are differences between c# and c++, but if I write my add2numbers function like

[code]
extern "C" __declspec(dllexport) double add2numbers(double a, double b)
{
return a + b;
}
[code]

then the aliasname is just add2numbers instead of the mangled ("decorated") name I presented in my first reply.

Of course you need to know the function arguments by type and its return type.
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Can macros using a dll written in c#

Postby rudolfo » Wed Aug 03, 2011 8:16 pm

Or open your mind a bit and look at it from a different point of view. In OpenOffice you are not bound to Basic as macro language as you are with the Microsoft Office suite. As far as I know there is a C#-Uno bridge which can be used to programm the UNO objects of OpenOffice. You can work in your familiar C# development environment and should have no problems at all to use code from within Dlls (whatever you can load with C# or the .NET framework).

 Edit: I guess I should be a bit more constructive. So please look in the External Programs subforum of the Macro forum for some ideas how to work with C# 
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Can macros using a dll written in c#

Postby samchanfs » Thu Aug 04, 2011 9:39 am

to Charlie
the dll is my boss give, so I can't change to use c++. And i can't find any code can make c# dll use like this

Code: Select all   Expand viewCollapse view
extern "C" __declspec(dllexport) double add2numbers(double a, double b)
{
return a + b;
}
OpenOffice 3.3 on Windows XP
samchanfs
 
Posts: 14
Joined: Wed Aug 03, 2011 7:33 am

Re: [SOLVED]Can macros using a dll written in c#

Postby rudolfo » Fri Aug 05, 2011 7:36 pm

samschanfs,
you have marked this thread as solved. But unfortunately if others are looking at this thread they won't see a solution.
So it would be nice, if you add a short comment how you solved it. Did your boss modify the calling convention in the C# Dll? Or did you find another way to use the Dll?
A forum can't live without questions. But it won't survive very long without answers. So please think from the point of view of a community project and add this little more effort to make this thread more helpful for others.

Thanks for the cooperation.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: [Solved] Can macros using a dll written in c#

Postby samchanfs » Fri Aug 12, 2011 6:17 am

it need register to com object and use following code to call.
sub TestDll
bj = CreateObject("test.Class1")
msgbox bj.test()
End sub
OpenOffice 3.3 on Windows XP
samchanfs
 
Posts: 14
Joined: Wed Aug 03, 2011 7:33 am


Return to Calc

Who is online

Users browsing this forum: No registered users and 10 guests