Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Using DLL from C#

Hi everybody,

I'm trying to get access to a selfdeveloped DLL. What I did:

I created a new Classobject in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ZufallArray
{
public class ZufallArray
{
[ExportDllAttribute.ExportDll("ZArray",
System.Runtime.InteropServices.CallingConvention.C decl)]
public static int[] ZArray (int beginn, int ende, int anzahl)
{
int[] ZZahl = new int[anzahl];
Random randObj = new Random();
for (int j = 0; j < anzahl; j++)
{
ZZahl[j]= randObj.Next(beginn, ende);
}
return ZZahl;
}
}
}

I exported the function with "ExportDLL"
(http://www.codeproject.com/KB/dotnet/DllExport.aspx) and checked the result
with Dependency Walker.

I used the function with VBA (Excel):


Declare Function Zar Lib _
"C:\........\bin\Release\ZufallArray.dll" _
Alias "ZArray" (ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
As Integer()

Sub zarray()
Dim a() As Integer
a = Zar(10, 20, 3)
For i = 1 To UBound(a)
MsgBox a(i)
Next i
End Sub

Excel crashes! Why?

P.S.: I don't want to use the function by using a reference to .... .tlb and
than creating a new class in Excel (in this case the function must be defined
without "static). By the way this is working pretty good.

I want to use the function this way, because if this works, I can use the
function in other applications.

Peter

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 78
Default Using DLL from C#

Hallo Peter,

I can not recreate your C# DLL file, but dit you register the DLL
file?

Wouter.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Using DLL from C#

Hallo,

I did not! I don't like to register the DLL because I want to use this DLL
on other machines without a need of administration privileges.

It's not necessary to register the DLL (only possible with ActiveX) if you
use the function inside the DLL with "Declare function ...."

Peter

"RadarEye" wrote:

Hallo Peter,

I can not recreate your C# DLL file, but dit you register the DLL
file?

Wouter.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Using DLL from C#

Peter
It might be worth trying something less ambitious than arrays initially.
What about using your dll just to add 2 numbers and return a double?

VBA uses the __stdcall calling convention rather than __cdecl, but that
should get you a bad calling convention error rather than a crash.

C ints are equivalent to VBA Longs, I suspect that same is true for C#
- that could cause a crash. (a,b,c should be long not integer)

I don't know how arrays are laid out in .net but I can imagine it being
different to C and to what VBA expects.

What specific error are you getting? (access violation?)

Cheers
Simon
Excel development website: www.codematic.net


Peter S. wrote:
Hi everybody,

I'm trying to get access to a selfdeveloped DLL. What I did:

I created a new Classobject in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ZufallArray
{
public class ZufallArray
{
[ExportDllAttribute.ExportDll("ZArray",
System.Runtime.InteropServices.CallingConvention.C decl)]
public static int[] ZArray (int beginn, int ende, int anzahl)
{
int[] ZZahl = new int[anzahl];
Random randObj = new Random();
for (int j = 0; j < anzahl; j++)
{
ZZahl[j]= randObj.Next(beginn, ende);
}
return ZZahl;
}
}
}

I exported the function with "ExportDLL"
(http://www.codeproject.com/KB/dotnet/DllExport.aspx) and checked the result
with Dependency Walker.

I used the function with VBA (Excel):


Declare Function Zar Lib _
"C:\........\bin\Release\ZufallArray.dll" _
Alias "ZArray" (ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
As Integer()

Sub zarray()
Dim a() As Integer
a = Zar(10, 20, 3)
For i = 1 To UBound(a)
MsgBox a(i)
Next i
End Sub

Excel crashes! Why?

P.S.: I don't want to use the function by using a reference to .... .tlb and
than creating a new class in Excel (in this case the function must be defined
without "static). By the way this is working pretty good.

I want to use the function this way, because if this works, I can use the
function in other applications.

Peter

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Using DLL from C#

Hallo Simon,

of course I already tried "an incremental approach" to my desired solution:

c#-Code:

[ExportDllAttribute.ExportDll("Summe")]
public static int Summe(int A, int B, [MarshalAs( UnmanagedType.AnsiBStr)]
ref string os)
{
os = A.ToString() + B.ToString() + " ok";
return A + B;
}

VBA-Code:

Private Declare Function Summe Lib "MyLib.dll" (ByVal A As Long, ByVal B As
Long, ByRef S As String) As Long

Sub test()
Dim S As String
i = Summe(10, 10, S)
MsgBox S
End Sub

Works fine!!!!!!! The string is given back perfectly.

Peter

"Simon Murphy" wrote:

Peter
It might be worth trying something less ambitious than arrays initially.
What about using your dll just to add 2 numbers and return a double?

VBA uses the __stdcall calling convention rather than __cdecl, but that
should get you a bad calling convention error rather than a crash.

C ints are equivalent to VBA Longs, I suspect that same is true for C#
- that could cause a crash. (a,b,c should be long not integer)

I don't know how arrays are laid out in .net but I can imagine it being
different to C and to what VBA expects.

What specific error are you getting? (access violation?)

Cheers
Simon
Excel development website: www.codematic.net


Peter S. wrote:
Hi everybody,

I'm trying to get access to a selfdeveloped DLL. What I did:

I created a new Classobject in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ZufallArray
{
public class ZufallArray
{
[ExportDllAttribute.ExportDll("ZArray",
System.Runtime.InteropServices.CallingConvention.C decl)]
public static int[] ZArray (int beginn, int ende, int anzahl)
{
int[] ZZahl = new int[anzahl];
Random randObj = new Random();
for (int j = 0; j < anzahl; j++)
{
ZZahl[j]= randObj.Next(beginn, ende);
}
return ZZahl;
}
}
}

I exported the function with "ExportDLL"
(http://www.codeproject.com/KB/dotnet/DllExport.aspx) and checked the result
with Dependency Walker.

I used the function with VBA (Excel):


Declare Function Zar Lib _
"C:\........\bin\Release\ZufallArray.dll" _
Alias "ZArray" (ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
As Integer()

Sub zarray()
Dim a() As Integer
a = Zar(10, 20, 3)
For i = 1 To UBound(a)
MsgBox a(i)
Next i
End Sub

Excel crashes! Why?

P.S.: I don't want to use the function by using a reference to .... .tlb and
than creating a new class in Excel (in this case the function must be defined
without "static). By the way this is working pretty good.

I want to use the function this way, because if this works, I can use the
function in other applications.

Peter




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Using DLL from C#

Did changing your VBA integers to longs help then?

Cheers
Simon
Excel development website: www.codematic.net


Peter S. wrote:
Hallo Simon,

of course I already tried "an incremental approach" to my desired solution:

c#-Code:

[ExportDllAttribute.ExportDll("Summe")]
public static int Summe(int A, int B, [MarshalAs( UnmanagedType.AnsiBStr)]
ref string os)
{
os = A.ToString() + B.ToString() + " ok";
return A + B;
}

VBA-Code:

Private Declare Function Summe Lib "MyLib.dll" (ByVal A As Long, ByVal B As
Long, ByRef S As String) As Long

Sub test()
Dim S As String
i = Summe(10, 10, S)
MsgBox S
End Sub

Works fine!!!!!!! The string is given back perfectly.

Peter

"Simon Murphy" wrote:

Peter
It might be worth trying something less ambitious than arrays initially.
What about using your dll just to add 2 numbers and return a double?

VBA uses the __stdcall calling convention rather than __cdecl, but that
should get you a bad calling convention error rather than a crash.

C ints are equivalent to VBA Longs, I suspect that same is true for C#
- that could cause a crash. (a,b,c should be long not integer)

I don't know how arrays are laid out in .net but I can imagine it being
different to C and to what VBA expects.

What specific error are you getting? (access violation?)

Cheers
Simon
Excel development website: www.codematic.net


Peter S. wrote:
Hi everybody,

I'm trying to get access to a selfdeveloped DLL. What I did:

I created a new Classobject in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ZufallArray
{
public class ZufallArray
{
[ExportDllAttribute.ExportDll("ZArray",
System.Runtime.InteropServices.CallingConvention.C decl)]
public static int[] ZArray (int beginn, int ende, int anzahl)
{
int[] ZZahl = new int[anzahl];
Random randObj = new Random();
for (int j = 0; j < anzahl; j++)
{
ZZahl[j]= randObj.Next(beginn, ende);
}
return ZZahl;
}
}
}

I exported the function with "ExportDLL"
(http://www.codeproject.com/KB/dotnet/DllExport.aspx) and checked the result
with Dependency Walker.

I used the function with VBA (Excel):


Declare Function Zar Lib _
"C:\........\bin\Release\ZufallArray.dll" _
Alias "ZArray" (ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
As Integer()

Sub zarray()
Dim a() As Integer
a = Zar(10, 20, 3)
For i = 1 To UBound(a)
MsgBox a(i)
Next i
End Sub

Excel crashes! Why?

P.S.: I don't want to use the function by using a reference to .... .tlb and
than creating a new class in Excel (in this case the function must be defined
without "static). By the way this is working pretty good.

I want to use the function this way, because if this works, I can use the
function in other applications.

Peter

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Using DLL from C#

No! I tried every combination (all integer, all long, some interger and some
long etc).

Peter

"Simon Murphy" wrote:

Did changing your VBA integers to longs help then?

Cheers
Simon
Excel development website: www.codematic.net


Peter S. wrote:
Hallo Simon,

of course I already tried "an incremental approach" to my desired solution:

c#-Code:

[ExportDllAttribute.ExportDll("Summe")]
public static int Summe(int A, int B, [MarshalAs( UnmanagedType.AnsiBStr)]
ref string os)
{
os = A.ToString() + B.ToString() + " ok";
return A + B;
}

VBA-Code:

Private Declare Function Summe Lib "MyLib.dll" (ByVal A As Long, ByVal B As
Long, ByRef S As String) As Long

Sub test()
Dim S As String
i = Summe(10, 10, S)
MsgBox S
End Sub

Works fine!!!!!!! The string is given back perfectly.

Peter

"Simon Murphy" wrote:

Peter
It might be worth trying something less ambitious than arrays initially.
What about using your dll just to add 2 numbers and return a double?

VBA uses the __stdcall calling convention rather than __cdecl, but that
should get you a bad calling convention error rather than a crash.

C ints are equivalent to VBA Longs, I suspect that same is true for C#
- that could cause a crash. (a,b,c should be long not integer)

I don't know how arrays are laid out in .net but I can imagine it being
different to C and to what VBA expects.

What specific error are you getting? (access violation?)

Cheers
Simon
Excel development website: www.codematic.net


Peter S. wrote:
Hi everybody,

I'm trying to get access to a selfdeveloped DLL. What I did:

I created a new Classobject in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ZufallArray
{
public class ZufallArray
{
[ExportDllAttribute.ExportDll("ZArray",
System.Runtime.InteropServices.CallingConvention.C decl)]
public static int[] ZArray (int beginn, int ende, int anzahl)
{
int[] ZZahl = new int[anzahl];
Random randObj = new Random();
for (int j = 0; j < anzahl; j++)
{
ZZahl[j]= randObj.Next(beginn, ende);
}
return ZZahl;
}
}
}

I exported the function with "ExportDLL"
(http://www.codeproject.com/KB/dotnet/DllExport.aspx) and checked the result
with Dependency Walker.

I used the function with VBA (Excel):


Declare Function Zar Lib _
"C:\........\bin\Release\ZufallArray.dll" _
Alias "ZArray" (ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
As Integer()

Sub zarray()
Dim a() As Integer
a = Zar(10, 20, 3)
For i = 1 To UBound(a)
MsgBox a(i)
Next i
End Sub

Excel crashes! Why?

P.S.: I don't want to use the function by using a reference to .... .tlb and
than creating a new class in Excel (in this case the function must be defined
without "static). By the way this is working pretty good.

I want to use the function this way, because if this works, I can use the
function in other applications.

Peter


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Using DLL from C#


Peter
Did you try passing the array as a variant?

Or maybe passing the array in by ref from VB as a parameter and just
populate the array in C#?

Cheers
Simon
Excel development website: www.codematic.net


Peter S. wrote:
No! I tried every combination (all integer, all long, some interger and some
long etc).

Peter

"Simon Murphy" wrote:

Did changing your VBA integers to longs help then?

Cheers
Simon
Excel development website: www.codematic.net


Peter S. wrote:
Hallo Simon,

of course I already tried "an incremental approach" to my desired solution:

c#-Code:

[ExportDllAttribute.ExportDll("Summe")]
public static int Summe(int A, int B, [MarshalAs( UnmanagedType.AnsiBStr)]
ref string os)
{
os = A.ToString() + B.ToString() + " ok";
return A + B;
}

VBA-Code:

Private Declare Function Summe Lib "MyLib.dll" (ByVal A As Long, ByVal B As
Long, ByRef S As String) As Long

Sub test()
Dim S As String
i = Summe(10, 10, S)
MsgBox S
End Sub

Works fine!!!!!!! The string is given back perfectly.

Peter

"Simon Murphy" wrote:

Peter
It might be worth trying something less ambitious than arrays initially.
What about using your dll just to add 2 numbers and return a double?

VBA uses the __stdcall calling convention rather than __cdecl, but that
should get you a bad calling convention error rather than a crash.

C ints are equivalent to VBA Longs, I suspect that same is true for C#
- that could cause a crash. (a,b,c should be long not integer)

I don't know how arrays are laid out in .net but I can imagine it being
different to C and to what VBA expects.

What specific error are you getting? (access violation?)

Cheers
Simon
Excel development website: www.codematic.net


Peter S. wrote:
Hi everybody,

I'm trying to get access to a selfdeveloped DLL. What I did:

I created a new Classobject in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ZufallArray
{
public class ZufallArray
{
[ExportDllAttribute.ExportDll("ZArray",
System.Runtime.InteropServices.CallingConvention.C decl)]
public static int[] ZArray (int beginn, int ende, int anzahl)
{
int[] ZZahl = new int[anzahl];
Random randObj = new Random();
for (int j = 0; j < anzahl; j++)
{
ZZahl[j]= randObj.Next(beginn, ende);
}
return ZZahl;
}
}
}

I exported the function with "ExportDLL"
(http://www.codeproject.com/KB/dotnet/DllExport.aspx) and checked the result
with Dependency Walker.

I used the function with VBA (Excel):


Declare Function Zar Lib _
"C:\........\bin\Release\ZufallArray.dll" _
Alias "ZArray" (ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
As Integer()

Sub zarray()
Dim a() As Integer
a = Zar(10, 20, 3)
For i = 1 To UBound(a)
MsgBox a(i)
Next i
End Sub

Excel crashes! Why?

P.S.: I don't want to use the function by using a reference to .... .tlb and
than creating a new class in Excel (in this case the function must be defined
without "static). By the way this is working pretty good.

I want to use the function this way, because if this works, I can use the
function in other applications.

Peter

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:08 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"