473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Accessing Unmanaged Data (C++) from .NET

Since C# and VB.NET are similar, I am posting this here also

I have a C++ DLL that I need to access from VB.NET or C#. The
following is my attempt via VB.NET.

The function I am trying to call is declared like this:

void* PASCAL iidc_lockdata( HIIDC, long frame = -1 )

This function returns a pointer to unsigned chars

I am able to find the size of the array out. It is usually 128800 to
use as an example if it helps.

I have declared the interface for my VB.NET as follows:

Private Declare Function _iidc_lockdata Lib "iidcapi_SONY.dll" Alias
"#37" (ByVal HIIDC As Int32, ByVal frame As Int32) As
System.Runtime.InteropServices.GCHandle

and also I have tried it as

Private Declare Function _iidc_lockdata Lib "iidcapi_SONY.dll" Alias
"#37" (ByVal HIIDC As Int32, ByVal frame As Int32) As IntPtr

(1) My first question is, how do I declare the function, should I use
GCHandle or IntPtr or something else?

As far as calling the function goes, I have tried this:

Dim gch As System.Runtime.InteropServices.GCHandle
gch = New System.Runtime.InteropServices.GCHandle
Dim lBufSz As Int32 = camera.currentDataFrameBytes(m_hCamera)
gch.Alloc(lBufSz)
Try
gch = camera.lockdata(m_hCamera, -1)
gch.Free()
Catch ex As Exception
MsgBox(ex.Message)
End Try

FYI the function camera.lockdata is a mapped function and it is as
follows:

Public Function lockdata(ByVal HIIDC As Int32, ByVal frame As Int32)
As System.Runtime.InteropServices.GCHandle
Return _iidc_lockdata(HIIDC, frame)
End Function
and I get "Object reference not set to an instance of an object"

(2) How would I get the data?

I have been really struggling with this.

Thanks,
Mike Dixon

Jun 7 '06 #1
1 1804
Mike,

See inline:
void* PASCAL iidc_lockdata( HIIDC, long frame = -1 )

This function returns a pointer to unsigned chars

I am able to find the size of the array out. It is usually 128800 to
use as an example if it helps.
Unless the function has some sort of character sequence, then you won't
be able to. In other words, C++ doesn't know about the length of the block
of memory returned (well, in COM, it does, but that's a different story).

So, what functions usually do is they indicate that there is a sequence
in the memory indicating that it is the end of the block returned. You need
to do that here (find out what the block terminator is).
I have declared the interface for my VB.NET as follows:

Private Declare Function _iidc_lockdata Lib "iidcapi_SONY.dll" Alias
"#37" (ByVal HIIDC As Int32, ByVal frame As Int32) As
System.Runtime.InteropServices.GCHandle

and also I have tried it as

Private Declare Function _iidc_lockdata Lib "iidcapi_SONY.dll" Alias
"#37" (ByVal HIIDC As Int32, ByVal frame As Int32) As IntPtr
I would do it as the second one, with one change. Declare HIIDC as an
IntPtr. It looks like a handle to me. The frame parameter looks right.
You should also return an IntPtr. GCHandle is for managed constructs, not
unmanaged ones.
(1) My first question is, how do I declare the function, should I use
GCHandle or IntPtr or something else?

As far as calling the function goes, I have tried this:

Dim gch As System.Runtime.InteropServices.GCHandle
gch = New System.Runtime.InteropServices.GCHandle
Dim lBufSz As Int32 = camera.currentDataFrameBytes(m_hCamera)
gch.Alloc(lBufSz)
Try
gch = camera.lockdata(m_hCamera, -1)
gch.Free()
Catch ex As Exception
MsgBox(ex.Message)
End Try

FYI the function camera.lockdata is a mapped function and it is as
follows:

Public Function lockdata(ByVal HIIDC As Int32, ByVal frame As Int32)
As System.Runtime.InteropServices.GCHandle
Return _iidc_lockdata(HIIDC, frame)
End Function
and I get "Object reference not set to an instance of an object"

(2) How would I get the data?
Once you have that pointer in managed code, you are going to have to
cycle through the buffer, byte by byte, until you come across your
terminating sequence. Then you will know the length.

You have one other problem here. The memory was allocated and returned
to you. You need to pass that handle off eventually to another method to
free the memory allocated.

As a side note, this is a horrendously designed function. While some
people might not like the Windows API, it is brilliant in that it always
makes the managing of memory the responsibility of the user, unless the
rules of memory management are explicitly stated (as it is in COM).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

I have been really struggling with this.

Thanks,
Mike Dixon

Jun 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Farmer | last post by:
I am having problems accessing a managed object from unmanaged code. Specifically, I have a .NET forms application with a web service proxy. The forms application links to an unmanaged DLL which...
2
by: Brett | last post by:
We have an application completely written in C++ under visual studio v6. One of our customers wants to use some of our existing components in his project but doesn't know C++ and isn't interested...
6
by: TB | last post by:
I understand the basics of finalization and implementing IDisposable and how one is guaranteed access to managed objects only when working through the IDisposable interface. My question is to what...
2
by: Brett Styles | last post by:
Hi Guys, I am trying to access a class in an unmanaged dll. I have created a wrapper managed class to access the functions I need but no matter what I try from the MSDN samples I can not get it to...
4
by: Bae,Hyun-jik | last post by:
Hi, What is the best case for accessing managed code from unmanaged C++ code, considering that the unmanaged code doesn't have managed extension? Please reply. Thanks in advance. Regards,...
3
by: Alex | last post by:
I'm having a problem porting an ASP solution to ASPX. In the ASP solution I'm accessing a DCOM server, create sub DCOM objects and call functions from VB script on the ASP pages. The DCOM object...
1
by: Stephan Zaubzer | last post by:
Hi I relatively new to C# and at the moment I am having troubles accessing com objects within C#. I am working in VS.net. I add the com library I want to access to my references. Accessing...
5
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the...
1
by: omantawy | last post by:
Hi, I have some legacy ASP web applications that use an unmanaged COM component to connect to a third party application. The third part application has moved to the managed code in the current...
4
by: Mau | last post by:
Hi, I have a managed dll which I am accessing from an unmanaged exe using Com Interop. I installed everything on computer 1 and run the unmanaged exe and from exe called my managed dll and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.