Using function from extern dll

Using function from extern dll

Page 1 of 1

5 Replies - 3072 Views - Last Post: 01 November 2009 - 04:23 AM Rate Topic: -----

#1 csrss   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-October 09

Using function from extern dll

Post icon  Posted 25 October 2009 - 11:30 AM

Hello. Actually i dunno how to name this topic right but my question is following:
I got my dll, written in win32 API and compiled nicely, which exports some function that my C# app needs very much.
Function looks like this:
DWORD Function(PULONG Table, PULONG Number);


Now i need to use it in my C# app. I know how to do it. But here is a problem. My function returns 2 unsinged long values: 1st is table which may contains integers, hex values, etcetera; and second is just a number. How we are doing it in win32 style? Like this right:

ULONG Table[256];
ULONG Number;
Function(&Table, &Number);


And we got our values. Now in C# i got following code:

 [DllImport("C:\\Users\\Administrator\\Desktop\\DLL\\DLL.dll", CharSet = CharSet.Auto)]
	   public static extern UInt32 Function(
			ref UInt32[] Table,
			ref UInt32 Number
			);

UInt32[] Table = new UInt32[256];
UInt32 Number = new UInt32();
Function(ref Table, ref Number);


This causes application crash :( The only good value returned is single number but table is not returned at all and really i got no idea how to do it in C# :( any help would be greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Using function from extern dll

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6258
  • View blog
  • Posts: 24,026
  • Joined: 23-August 08

Re: Using function from extern dll

Posted 25 October 2009 - 01:53 PM

Try searching for pinvoke or P/Invoke.
Was This Post Helpful? 0
  • +
  • -

#3 csrss   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-October 09

Re: Using function from extern dll

Posted 25 October 2009 - 02:50 PM

I know how to call unamanaged function from managed code, i know how to call win32, pure native functions from C# code. My function works, but works like in 50% - it have to return number and table. It returns only number, in case of table it returns some crazy long long integer and application crashes. If i make it return (and fetch from win32 dll) only the number - then it works cool. So the problem lies in working with tables in C# in case of my function. Well, i am working with C# for about a week or lees now (anyway i am programming for a couple of years) - i got basics but still there is a few things that i am out of idea about.
Was This Post Helpful? 0
  • +
  • -

#4 Renagado   User is offline

  • D.I.C Regular
  • member icon

Reputation: 118
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Using function from extern dll

Posted 25 October 2009 - 05:05 PM

So when the app crashes, what error message do you get exactly?
Was This Post Helpful? 0
  • +
  • -

#5 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Using function from extern dll

Posted 25 October 2009 - 06:03 PM

I'm not sure if this really would make a difference, but in C# a ULONG is a UInt64...
Was This Post Helpful? 0
  • +
  • -

#6 crepitus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Using function from extern dll

Posted 01 November 2009 - 04:23 AM

View PostlesPaul456, on 25 Oct, 2009 - 05:03 PM, said:

I'm not sure if this really would make a difference, but in C# a ULONG is a UInt64...


The Windows data type ULong is 32 bits, so that part is ok.

Back to the Original Poster's question...

My function returns 2 unsinged long values

The function returns one DWORD. I guess you mean the ref parameters.

1st is table which may contains integers, hex values, etcetera

What is Table? Is it one 32 bit value, or is it an array? You are treating it as an array in your C# code, so I guess the PULONG is a pointer to the first value of an array. If so, you need to apply marshaling attributes to the p/invoke signature. See msdn: Default Marshalling for Arrays.

Maybe this would work:

[DllImport("C:\\Users\\Administrator\\Desktop\\DLL\\DLL.dll", CharSet = CharSet.Auto)]
public static extern UInt32 Function(
   [MarshalAs(UnmanagedType.LPArray, SizeConst=256)]  UInt32[] Table,
   ref UInt32 Number);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1