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,

thanks a lot for your coding! Why didn't I do that in the way you show? I
use Unify Gupta SQLBase which is a very stable database, but it's a little
bit tricky to use some function there. A function to get random numbers is
not available there. So I have to code a function (C#, C++, VB6 etc.) and
store it in a DLL. If I can use the function in VBA by "Declare function
......." I can use it in SQLBase.

In the meantime I got the result:

Code in C#:

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

namespace classtest
{
public class test
{
[ExportDllAttribute.ExportDll("ArrayTest",
System.Runtime.InteropServices.CallingConvention.S tdCall)]
[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType =
VarEnum.VT_I4)]
public static int[] ArrayTest(int beginn, int ende, int anzahl)
{
int[] ZZahl = new int[anzahl+1];
Random randObj = new Random();
for (int j = 0; j < ZZahl.Length; 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):

Private Declare Function classTArray Lib _
"C:\........\bin\Release\classtarray.dll" _
Alias "ArrayTest" (ByVal nAnfang As Long, ByVal nEnde As Long, ByVal nAnzahl
As Long) As Long()

Sub test()
Dim x() As Long
x = classTArray(0, 100, 10)
For i = 0 To 10
MsgBox x(i)
Next i
End Sub

Works fine!

Peter

"RadarEye" wrote:

Hi Peter,

I have recreated yout problem.
I have tried to create a log-file from the DLL, but dit not succeed in
this.

Why are you ussing the DLL.
Have you considered creating this fucntion in a separate module in
your Excel file and export it.
You can import it in other Excel File which need the same function.

Option Explicit
Option Base 1

Public Function FilledArray(intBegin As Integer, _
intEnde As Integer, _
intAnzahl As Integer) As Variant
ReDim intArray(intAnzahl) As Integer
Dim intWiederholung As Integer

Randomize Timer
For intWiederholung = 0 To intAnzahl - 1
intArray(intWiederholung) = intBegin + _
Fix(Rnd() * (intEnde - intBegin))
Next

FilledArray = intArray
End Function

HTH,
Wouter

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:00 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"