Passing 2D array to C dll
Passing 2D array to C dll
Passing 2D array to C dll
Passing 2D array to C dll
Passing 2D array to C dll
Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll
Passing 2D array to C dll Passing 2D array to C dll
Passing 2D array to C dll
Go Back  Xtreme Visual Basic Talk > > > Passing 2D array to C dll


Reply
 
Thread Tools Display Modes
  #1  
Old 08-03-2009, 12:35 PM
-clueless -clueless is offline
Newcomer
 
Join Date: Jul 2009
Posts: 6
Default Passing 2D array to C dll


I am trying to pass a 2D array into a dll I wrote in C. When I run the program, it crashes at the first line (in the dll) that I try to read the array. It gives an error that it is trying to read protected memory. I'm guessing this means the array was not correctly passed to the dll, so the program is trying to read a memory location that doesn't exist. Is the problem somewhere in my VB code? If not, it must be in my C code, right?

Here is my VB code:

Declaration of the function in the dll:
Code:
Private Declare Function image_processing Lib "image_processingDLL.dll" _
                        (ByRef image_array As Long, ByVal dpi As Long, _
                        ByVal xDim As Long, ByVal yDim As Long) As result
Calling the function:
Code:
Call image_processing(hImgArray(0, 0), hImage.HorizontalResolution, _
                      hFilmPictureBox.Width, hFilmPictureBox.Height)
If someone could confirm that this is correct, or tell me it's wrong it would me much appreciated.

Thanks,

~I'm Clueless
Reply With Quote
  #2  
Old 08-03-2009, 01:03 PM
AtmaWeapon's Avatar
AtmaWeaponPassing 2D array to C dll AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

You didn't declare your array parameter as an array, try this or something similar (I almost never use 2D arrays so my syntax is rusty):
ByRef image_array(,) As Long
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 08-03-2009, 02:03 PM
darkforcesjedi's Avatar
darkforcesjediPassing 2D array to C dll darkforcesjedi is offline
Trust me, I'm an

* Expert *
 
Join Date: Apr 2001
Location: In ur base, pwnin d00dz
Posts: 1,964
Default

Shouldn't the ByRef pass a pointer to the first element passed in the call to the function (in the example call, the first element)?

Also, I have a feeling those "Long"s are really supposed to be Integers (32-bit integers). Also, what is the return value ("result")? My guess is the problem arises from the array being located in managed memory.
__________________
To err is human; to debug, divine.
Reply With Quote
  #4  
Old 08-03-2009, 02:47 PM
-clueless -clueless is offline
Newcomer
 
Join Date: Jul 2009
Posts: 6
Default

Oh, sorry AtmaWeapon, I forgot to include that. Here is the declaration:

Dim hImgArray(,) As Integer

This is a dynamic array. Here is the other part:

ReDim hImgArray(0 To xmax, 0 To ymax)

where xmax and ymax are known values.

darkforcesjedi, yes, I think the ByRef does pass a pointer to the first element. Isn't that what I want to do?
As far as Int vs Long goes, an article I read said,
"VB's Integer type is actually only 16 bits long and maps to C's short type. Likewise, C's int type is 32 bits long, which means you should use a Long on the VB end." (http://articles.techrepublic.com.com...1-1045686.html)
The return value "result" is a structure defined in the dll.
What is managed memory, and how can I change/work around it?

Thanks,

~I'm Clueless
Reply With Quote
  #5  
Old 08-03-2009, 04:26 PM
AtmaWeapon's Avatar
AtmaWeaponPassing 2D array to C dll AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
Originally Posted by darkforcesjedi View Post
Shouldn't the ByRef pass a pointer to the first element passed in the call to the function (in the example call, the first element)?

Also, I have a feeling those "Long"s are really supposed to be Integers (32-bit integers). Also, what is the return value ("result")? My guess is the problem arises from the array being located in managed memory.
Right, it passes a pointer, but it's a pointer to the first element of the array in managed memory. Unlike in unmanaged memory, variables don't always stay put. So if you don't tell .NET that you're marshalling an array, it doesn't know that it needs to copy that array to unmanaged memory and you get a pointer to something that may or may not be what you want when it's accessed. In particular, I'd expect the CLR to get agitated when unmanaged code tries to poke around in its managed memory. This signature might work if you allocate some unmanaged memory, copy the array into it, then pass the pointer to that memory, but it'd be easier to just tell .NET it's marshalling a 2D array and let it do the work.

I could be wrong, but this is my speculation.

-clueless, you misunderstood my post. I didn't mean "I don't see you declaring the array anywhere", I meant "The first parameter to your method indicates that you are passing a 64-bit integer by reference. You have said that the function expects a 2D array of 64-bit integers; you should fix your parameter in the P\Invoke declaration."

With respect to the bitness of the integers, you read an article about VB6, not VB .NET. As a general rule, if an article was published before 2003 it's probably not very relevant for .NET development, if at all. Make sure you are looking at an article about VB .NET before following it. In .NET, VB was brought up to date with the 90s and uses a 32-bit integer for Integer. Long is a 64-bit integer.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 08-04-2009, 10:09 AM
-clueless -clueless is offline
Newcomer
 
Join Date: Jul 2009
Posts: 6
Default

Thanks for the info. I didn't know there was a difference (I'm kinda new at all this).
I have built a much simpler project to troubleshoot the problem, and have made the changes you suggested (except the part about managed and unmanaged memory and marshaling a 2D array, I don't know what any of that means, sorry).

Here is the new problem: I have a 2x2 array of ints with a pointer being passed into the dll. All the dll does is return 1 of the elements. I get the right value when I return [0][0] and [0][1], but when I return [1][0] and [1][1] I get garbage. Furthermore, I can return element [0][2] and [0][3] and I get what I would expect from [1][0] and [1][1]. (This is a 2x2 array so there shouldn't be elements at [0][2] or [0][3].)
Here is the VB code:
Code:
    Dim array(0 To 1, 0 To 1) As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        array(0, 0) = 100
        array(0, 1) = 200
        array(1, 0) = 300
        array(1, 1) = 400
    End Sub

    Private Declare Function myfunction Lib "c:\code\test\mydll.dll" (ByRef array(,) As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = CStr(myfunction(array))
    End Sub
So, I think the problem is that VB and C setup and access arrays differently. Does this make sense, or is there something else wrong? Any ideas on how to fix it? Maybe there is a way to force either VB or C to arrange or read the memory differently? Or maybe it does have something to do with managed/unmanaged memory.

Thanks for all the help so far.

~I'm Clueless
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Passing 2D array to C dll
Passing 2D array to C dll
Passing 2D array to C dll Passing 2D array to C dll
Passing 2D array to C dll
Passing 2D array to C dll
Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll Passing 2D array to C dll
Passing 2D array to C dll
Passing 2D array to C dll
 
Passing 2D array to C dll
Passing 2D array to C dll
 
-->