Hello,
I am trying to get some data from a C Dll via a structure and I am getting incorrect values back. I tried a couple of examples I found but they do not work. Since I do not know the proper way. I do not know how to fix it. Can anyone give me any advice, hints, redirection or any assistance what-so-ever. I've been struggling with this for weeks now so I will appreciate any kind of guidance (so will my boss :).
I found out that a pointer to a structure is not the same as a pointer to the memory that the structure occupies. Changed my code to copy the members explicitly. -
Dim rc As twRC
-
Dim pOneValue As IntPtr
-
Dim pPosition As Int32 = 0
-
Dim strValue As String
-
Dim twFix32 As twsFix32
-
Dim twOneValue As TW_ONEVALUE
-
-
-
rc = DS_Capability(mtwApp, mtwSource, twDG.Control, twDAT.Capability, twMSG.Get, twCapability)
-
If rc = twRC.Success Then
-
-
twOneValue = New TW_ONEVALUE
-
pOneValue = GlobalLock(twCapability.Handle)
-
twOneValue.ItemType = CType(Runtime.InteropServices.Marshal.ReadInt16(pOneValue, pPosition), ItemType)
-
pPosition += 2
-
-
Select Case twOneValue.ItemType
-
-
Case ItemType.Fix32
-
Dim origFrac As Int32
-
twFix32 = New twsFix32
-
-
twOneValue.Item = Marshal.ReadInt16(pOneValue, pPosition)
-
twFix32.Whole = twOneValue.Item
-
pPosition += 2
-
-
-
origFrac = Marshal.ReadInt16(pOneValue, pPosition)
-
If origFrac < 0 Then
-
origFrac += 65536
-
End If
-
pPosition += 2
-
-
'twFix32.Frac = CUShort(origFrac)
-
strValue = (twFix32.Whole + (origFrac / 65536)).ToString
-
-
Case Else
-
MsgBox("Unexpected One Value. Ignored")
-
-
End Select
-
-
GlobalUnlock(twCapability.Handle)
-
-
Return strValue
-
-
End If
3 5310
Thanks for the reply. For the record I searched through quite a few pages on this site which took a couple of hours and found NOTHING helpful. Perhaps no one used the same keywords I used, although it did pick up several quotes that contained the keywords which the article had no relation.
And that link you provided doesn't provide anything useful, besides it discusses C#, NOT VB. since the problem I am having involves passing data from one language to another, including a third unnecessarily complicates things. Perhaps I need to give more detailed information, here goes:
I am writing a VB.NET application that uses TWAIN (dumb dll). When I call the function and get data back it is incorrect. What I don't know is what I am doing wrong. Here are the C structures: -
typedef unsigned short TW_UINT16, FAR *pTW_UINT16;
-
typedef unsigned long TW_UINT32, FAR *pTW_UINT32;
-
typedef HANDLE TW_HANDLE;
-
-
typedef struct {
-
TW_UINT16 Cap; /* id of capability to set or get, e.g. CAP_BRIGHTNESS */
-
TW_UINT16 ConType; /* TWON_ONEVALUE, _RANGE, _ENUMERATION or _ARRAY */
-
TW_HANDLE hContainer; /* Handle to container of type Dat */
-
} TW_CAPABILITY, FAR * pTW_CAPABILITY;
-
-
typedef struct {
-
TW_UINT16 ItemType;
-
TW_UINT32 Item;
-
} TW_ONEVALUE, FAR * pTW_ONEVALUE;
-
-
Which have been converted to VB.NET structure/class: - <StructLayout(LayoutKind.Sequential, Pack:=4)> Private Structure twsCapability
-
Public Cap As twCap 'short
-
Public ConType As twContainerTyoe 'short
-
Public Handle As IntPtr 'Pointer to a container
-
End Structure
-
-
<StructLayout(LayoutKind.Sequential, Pack:=4)> Friend Class TW_ONEVALUE
-
Public ItemType As Short
-
Public Item As Integer
-
End Class
-
Then I run this routine and get back 64356640 for the physical height and width: - 'Call into the C library
-
<DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DS_Capability( _
-
<[In](), Out()> ByVal pOrigin As twsIdentity, _
-
<[In]()> ByVal pSource As twsIdentity, _
-
ByVal dg As twDG, _
-
ByVal dat As twDAT, _
-
ByVal msg As twMSG, _
-
<[In](), Out()> ByRef pData As twsCapability) As twRC
-
End Function
-
-
'Start here:
-
...previous code
-
twCapability = New twsCapability
-
With twCapability
-
.Cap = twCap.IPhysicalWidth
-
.ConType = twContainerTyoe.One 'TWON_DONTCARE16
-
'.Handle = Nothing
-
End With
-
strWidth = GetOneValue(twCapability)
-
-
With twCapability
-
.Cap = twCap.IPhysicalHeight
-
'.ConType = 7 'TWON_DONTCARE16
-
'.Handle = Nothing
-
End With
-
strHeight = GetOneValue(twCapability)
-
...more code
-
-
Private Function GetOneValue(ByVal twCapability As twsCapability) As String
-
-
Dim rc As twRC
-
Dim strTemp As String
-
Dim twOneValue As TW_ONEVALUE
-
-
-
rc = DS_Capability(mtwApp, mtwSource, twDG.Control, twDAT.Capability, twMSG.Get, twCapability)
-
If rc = twRC.Success Then
-
strTemp = vbNullString
-
Select Case twCapability.ConType
-
Case twContainerTyoe.One
-
twOneValue = New TW_ONEVALUE
-
Marshal.PtrToStructure(twCapability.Handle, twOneValue)
-
strTemp = CType(twOneValue.Item, String)
-
Case Else
-
MsgBox("Error, one value expected; " & twCapability.ConType & " returned.")
-
End Select
-
Marshal.FreeHGlobal(twCapability.Handle)
-
Return strTemp
-
Else
-
ErrorStatus()
-
Return vbNullString
-
End If
-
-
End Function
-
-
Private Function GetOneValueNew(ByVal twCapability As twsCapability) As String
-
-
Dim rc As twRC
-
Dim pOneValue As Integer
-
Dim pTemp As Integer
-
Dim strTemp As String
-
Dim twOneValue As TW_ONEVALUE
-
-
-
rc = DS_Capability(mtwApp, mtwSource, twDG.Control, twDAT.Capability, twMSG.Get, twCapability)
-
If rc = twRC.Success Then
-
strTemp = vbNullString
-
Select Case twCapability.ConType
-
Case twContainerTyoe.One
-
twOneValue = New TW_ONEVALUE
-
pOneValue = GlobalLock(twCapability.Handle)
-
pTemp = VarPtr(twOneValue)
-
CopyMemory(pTemp, pOneValue, Marshal.SizeOf(twOneValue))
-
GlobalUnlock(twCapability.Handle)
-
strTemp = CType(twOneValue.Item, String)
-
Case Else
-
MsgBox("Error, one value expected; " & twCapability.ConType & " returned.")
-
End Select
-
GlobalFree(twCapability.Handle)
-
Return strTemp
-
Else
-
ErrorStatus()
-
Return vbNullString
-
End If
-
-
End Function
-
I included the function GetOneValueNew( because I found that method somewhere on the net, but it was written in VB6. Unfortunately I get 0 back from that.
I tried searching the net but didn't find my answer. I used "VB.NET marshal structure C dll". I included this incase someone may have a better search string.
I even tried writing the code to access the dll in C++. But I get an error in the VB routine that calls it "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I presume that is also a problem form the unmanaged C dll trying to write to memory allocated in managed C++.
Thanks for looking!
I found out that a pointer to a structure is not the same as a pointer to the memory that the structure occupies. Changed my code to copy the members explicitly. -
Dim rc As twRC
-
Dim pOneValue As IntPtr
-
Dim pPosition As Int32 = 0
-
Dim strValue As String
-
Dim twFix32 As twsFix32
-
Dim twOneValue As TW_ONEVALUE
-
-
-
rc = DS_Capability(mtwApp, mtwSource, twDG.Control, twDAT.Capability, twMSG.Get, twCapability)
-
If rc = twRC.Success Then
-
-
twOneValue = New TW_ONEVALUE
-
pOneValue = GlobalLock(twCapability.Handle)
-
twOneValue.ItemType = CType(Runtime.InteropServices.Marshal.ReadInt16(pOneValue, pPosition), ItemType)
-
pPosition += 2
-
-
Select Case twOneValue.ItemType
-
-
Case ItemType.Fix32
-
Dim origFrac As Int32
-
twFix32 = New twsFix32
-
-
twOneValue.Item = Marshal.ReadInt16(pOneValue, pPosition)
-
twFix32.Whole = twOneValue.Item
-
pPosition += 2
-
-
-
origFrac = Marshal.ReadInt16(pOneValue, pPosition)
-
If origFrac < 0 Then
-
origFrac += 65536
-
End If
-
pPosition += 2
-
-
'twFix32.Frac = CUShort(origFrac)
-
strValue = (twFix32.Whole + (origFrac / 65536)).ToString
-
-
Case Else
-
MsgBox("Unexpected One Value. Ignored")
-
-
End Select
-
-
GlobalUnlock(twCapability.Handle)
-
-
Return strValue
-
-
End If
Sign in to post your reply or Sign up for a free account.
Similar topics
by: C. N. Sridhar |
last post by:
Hi,
I'm writing a wrapper to a win32 dll in C#. I need to call
a method in DLL which has a Variant type reference
parameter.
How to marshal variant type from win32 (unmanaged code)
to C#...
|
by: Nikolay Petrov |
last post by:
Guys, please help. I am trying to make this work from at least 4 months.
I am new to programming and some things are difficult to me, but I really
need to make my project work.
I can't find...
|
by: Vadym Stetsyak |
last post by:
Hi there!!!
I'm looking for any resources on the subject.
Any help will be appreciated!
--
Vadym Stetsyak
ICQ 161730125
He, who commands the past - commands the future
|
by: VM |
last post by:
What's marshalling? I've had to use it extensively for a project but I don't
know what it means. I tried to look for a definition in the Internet but I
couldn't find anything that would explain...
|
by: Nadav |
last post by:
Hi
I am about to write a performance crutial system, I am considering writing this system based on native COM or unmanaged C++ exposed as CLI, Now, I Wonder... does exposing a native code through...
|
by: Chua Wen Ching |
last post by:
Hi there,
I had some problems converting C -> C#.
C codes:
unsigned char BYTE;
unsigned short puint8;
typedef struct {
|
by: mirek |
last post by:
Hi,
I'm trying to import my old code to the .NET using managed wrappers.
I've read "Managed Extensions for C++ Migration Guide" document and was
trying to do what it stated in it. For example if...
|
by: Lee Crabtree |
last post by:
I have several unmanaged struct types that I've created equivalent managed
struct types for. How do I marshal the managed structs into their unmanaged
versions and vice versa?
Here's a quick...
|
by: Aston Martin |
last post by:
Hi All,
********************** My Situation **********************
I am working on project that involves passing a structure to unmanaged
code from .Net world (well using C#). Perhaps an example...
|
by: tomwolfstein |
last post by:
Hi.
I am trying to write a wrapper for the standard VC1 decoder, and I need to resolve a "TypeLoadException"
The decoder comes an an executable which I've turned into a .dll. This decoder has about...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |