473,396 Members | 1,738 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,396 software developers and data experts.

Calling unmanaged DLL issue

Hello,
I am trying to call an unmanaged API (IP helper) and having problems getting
started. I have the following code to read an IP Forward table. It appears
my first call to "GetIPForwardTable" appears to work, it returns the
required buffer size in the ByteCount variable. However when I try the
second call, the one that should actually read the table the application
simply exits. This suggests a stack corruption or similar. Can anyone see
what is wrong with the code?
Dim lpDataBuffer As IntPtr

Dim BufferSize As Int32

Dim ByteCount As Int32 = Marshal.SizeOf(BufferSize)

Dim Result As Int32

'

' create unmanged data buffer that is too small for real data,

' this will force the method to return the actual byte count needed

'

lpDataBuffer = Marshal.AllocHGlobal(2)

Try

'

' First do a read, the buffer size is too small, this

' will return the needed size in the buffer size buffer

'

Result = GetIpForwardTable(lpDataBuffer, ByteCount, True)

'

' Free the unmanged data buffer

'

Marshal.FreeHGlobal(lpDataBuffer)

'

' Create a new buffer for the real data

'

lpDataBuffer = Marshal.AllocHGlobal(5000)

'

' Read the table

'

Result = GetIpForwardTable(lpDataBuffer, ByteCount, True)

Catch ex As Exception

MsgBox("OOPS!")

End Try

Thanks for any pointers,

Sid.
Jun 27 '08 #1
2 1167
On 2008-05-02, Sid Price <si*@nowhere.comwrote:
Hello,
I am trying to call an unmanaged API (IP helper) and having problems getting
started. I have the following code to read an IP Forward table. It appears
my first call to "GetIPForwardTable" appears to work, it returns the
required buffer size in the ByteCount variable. However when I try the
second call, the one that should actually read the table the application
simply exits. This suggests a stack corruption or similar. Can anyone see
what is wrong with the code?
Dim lpDataBuffer As IntPtr

Dim BufferSize As Int32

Dim ByteCount As Int32 = Marshal.SizeOf(BufferSize)

Dim Result As Int32

'

' create unmanged data buffer that is too small for real data,

' this will force the method to return the actual byte count needed

'

lpDataBuffer = Marshal.AllocHGlobal(2)

Try

'

' First do a read, the buffer size is too small, this

' will return the needed size in the buffer size buffer

'

Result = GetIpForwardTable(lpDataBuffer, ByteCount, True)

'

' Free the unmanged data buffer

'

Marshal.FreeHGlobal(lpDataBuffer)

'

' Create a new buffer for the real data

'

lpDataBuffer = Marshal.AllocHGlobal(5000)

'

' Read the table

'

Result = GetIpForwardTable(lpDataBuffer, ByteCount, True)

Catch ex As Exception

MsgBox("OOPS!")

End Try

Thanks for any pointers,

Sid.

Sid,

It would really be nice to have a complete working example... That means
it helps to see your declare statements :)

--
Tom Shelton
Jun 27 '08 #2
"Sid Price" <si*@nowhere.comschrieb
Hello,
I am trying to call an unmanaged API (IP helper) and having problems
getting started. I have the following code to read an IP Forward
table. It appears my first call to "GetIPForwardTable" appears to
work, it returns the required buffer size in the ByteCount variable.
However when I try the second call, the one that should actually
read the table the application simply exits. This suggests a stack
corruption or similar. Can anyone see what is wrong with the code?

Most important: What's your declaration of GetIPForwardTable? The rest
looks ok at first sight.

Armin

Jun 27 '08 #3

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

Similar topics

9
by: Andrew Cumming | last post by:
I am having trouble using Platform Invoke to call C++ DLLs from C# windows applications. The DLLs seem to load, but I get run-time errors to the effect that the named entry points cannot be found....
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
6
by: William F. Kinsley | last post by:
I am thinking of porting an existing MFC application to MC++ and I have created a simple MFC application to test the environment. My sample MFC application is compilied with the /clr switch. I...
10
by: Gustavo L. Fabro | last post by:
Greetings! I've been porting an application for Builder to VS .NET 2003 and searching for possible bottlenecks (the application is currently running slow). I found out one scenario that takes a...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.