Page 1 of 1

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

Posted: Wed Aug 03, 2011 7:41 am
by samchanfs
i'm trying to use dll written in c# but, i don't know how to do it .
can anyone help me?
thx

Re: Can macros using a dll written in c#

Posted: Wed Aug 03, 2011 8:56 am
by Charlie Young
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

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

__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

?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

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.

Re: Can macros using a dll written in c#

Posted: Wed Aug 03, 2011 10:14 am
by samchanfs
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"

Re: Can macros using a dll written in c#

Posted: Wed Aug 03, 2011 6:37 pm
by Charlie Young
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: Select all

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.

Re: Can macros using a dll written in c#

Posted: Wed Aug 03, 2011 8:16 pm
by rudolfo
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# 

Re: Can macros using a dll written in c#

Posted: Thu Aug 04, 2011 9:39 am
by samchanfs
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

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

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

Posted: Fri Aug 05, 2011 7:36 pm
by rudolfo
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.

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

Posted: Fri Aug 12, 2011 6:17 am
by samchanfs
it need register to com object and use following code to call.
sub TestDll
bj = CreateObject("test.Class1")
msgbox bj.test()
End sub