473,508 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshalling Unmanaged DLL

Hi,

I've really flailed at this today, and read many posts and MSDN help
areas. I would really appreciate a sanity check.

The DLL is for OziExplorer API, the function declaration from the
documentation is:

function oziGetExePath(var Path:pansichar;
var DataLenth:integer):integer;stdcall;

I believe that PAnsiChar is a Delphi type for "pointer to Ansi Char"
and can be used similar to LPSTR to point at null terminated strings.

In general the API states return values as -1 for timeout, 0 for
completion, or other numbers as indicated by the specific funtion.
This function should return the path to the EXE file, so I expect it
should return only 0 or -1.

In C# I write the following:
________

StringBuilder sb = new StringBuilder(512);

[DllImport("OziAPI.dll", CharSet = CharSet.Ansi, CallingConvention =
CallingConvention.StdCall)]

static extern int oziGetExePath(ref StringBuilder s, ref int length);

public string getOziExePath()
{
int size = 0;
int ret;

ret=oziGetExePath(ref sb, ref size);
//if (ret != 0)
// throw new Exception("Could not get Ozi exe Path");
return (sb.ToString());
}
___________

1. While the return value is correct, the "ret" value is 1, so my API
error trap will always throw an exception. I'm a bit leery about
ignoring return values.

2. In examples I do not see the StringBuilder passed by reference. I
do not get the correct result if I do not pass by reference. I also
tried defining the path parameter as String. It also required a "ref"
and the behaviour is identical to using StringBuilder.

3. I think that the unmanaged code allocates a buffer, and I don't
want the CLR to try and release it. The API provides a "close"
function to free up storage, as it reuses it internally. I tried some
tricks with "IntPtr" and "Marshal.PtrToStringAuto()" that did not seem
to work either.

I would be happy to learn more from anyone in the group! Thanks in
advance for any help.

Regards,
Jim
Nov 16 '05 #1
2 3867
Id try again with IntPtr
Did you try Marshal.PtrToStringAnsi (using the size returned by the function)?

maybe you could try Marshal.Copy to get the data into a byte array and see
whats in it.

Does size return a value?

Scott
"Jim shedden" wrote:
Hi,

I've really flailed at this today, and read many posts and MSDN help
areas. I would really appreciate a sanity check.

The DLL is for OziExplorer API, the function declaration from the
documentation is:

function oziGetExePath(var Path:pansichar;
var DataLenth:integer):integer;stdcall;

I believe that PAnsiChar is a Delphi type for "pointer to Ansi Char"
and can be used similar to LPSTR to point at null terminated strings.

In general the API states return values as -1 for timeout, 0 for
completion, or other numbers as indicated by the specific funtion.
This function should return the path to the EXE file, so I expect it
should return only 0 or -1.

In C# I write the following:
________

StringBuilder sb = new StringBuilder(512);

[DllImport("OziAPI.dll", CharSet = CharSet.Ansi, CallingConvention =
CallingConvention.StdCall)]

static extern int oziGetExePath(ref StringBuilder s, ref int length);

public string getOziExePath()
{
int size = 0;
int ret;

ret=oziGetExePath(ref sb, ref size);
//if (ret != 0)
// throw new Exception("Could not get Ozi exe Path");
return (sb.ToString());
}
___________

1. While the return value is correct, the "ret" value is 1, so my API
error trap will always throw an exception. I'm a bit leery about
ignoring return values.

2. In examples I do not see the StringBuilder passed by reference. I
do not get the correct result if I do not pass by reference. I also
tried defining the path parameter as String. It also required a "ref"
and the behaviour is identical to using StringBuilder.

3. I think that the unmanaged code allocates a buffer, and I don't
want the CLR to try and release it. The API provides a "close"
function to free up storage, as it reuses it internally. I tried some
tricks with "IntPtr" and "Marshal.PtrToStringAuto()" that did not seem
to work either.

I would be happy to learn more from anyone in the group! Thanks in
advance for any help.

Regards,
Jim

Nov 16 '05 #2
"sfawcett" <sf******@discussions.microsoft.com> wrote in message news:<75**********************************@microso ft.com>...
Id try again with IntPtr
Did you try Marshal.PtrToStringAnsi (using the size returned by the function)?

maybe you could try Marshal.Copy to get the data into a byte array and see
whats in it.

Does size return a value?


Hi Scott,

Thanks for the reply. I do not have a problem getting the data back in
both the string and integer parameters. I was looking for a sanity
check on the most accestable way to do it, as I am new to C#.

My problem is the return value of the function. It doesn't make sense
to get back expected data, with a return value inconsistent with
proper execution. I've never used this DLL before, but I imagine the
author is reliable.

Regards,
Jim Shedden
Nov 16 '05 #3

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

Similar topics

3
2821
by: PHil Coveney | last post by:
Hello, I am having difficulty marshalling structures when calling DeviceIoControl. I am importing this Win32 function as static extern int DeviceIoControl (int hDevice, int...
1
1094
by: MuZZy | last post by:
Hello, I am probably facing a sort of language barrier, but i can't really get what 'marshalling' is in terms of .NET Is it a kind of type casting? And when do you use it? Thank you, Andrey
4
10664
by: TT (Tom Tempelaere) | last post by:
Hi people I am wrapping a C dll using PInvoke from C#. I need to wrap the following signature in C int dma_start( const UCHAR* data, UINT data_length ) The function should start with a DMA...
2
5770
by: Rookie | last post by:
Hi, I had a question on DllImport. On importing a function from a VC++ dll using DllImport (to a C# program), the function argument data types and the return types may be of a type that is not...
1
1624
by: halise | last post by:
hello, i am trying to wrap an unmanaged C++ code to managed one so that i can use it within c#, and i am using IJW - "It Just Works" - method for that. In the code, there is a function which...
1
4661
by: | last post by:
Hi, Is there any good links for datatype interop? I need to pass some structure pointers into an unmanaged method and return char* etc but having some problems in my C++/CLI proxy class. I...
2
1262
by: BartMan | last post by:
Greetings, When working with managed c++, do you have to do anything special when going from simple types from managed to unmanaged and vice versa. Or is marshalling handled automatically for...
3
6302
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
11
2674
by: Daniel Bass | last post by:
Greetings! I'm trying to call this method in a c# app... SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE DeviceHandle, unsigned char *pData, long max_length); So far I've got...
3
397
by: Saad | last post by:
Hi, I have a struct as follows: public __gc struct STTemp { public: int someint; System::Collections::ArrayList* arrTemp;
0
7225
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
7123
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...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
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
5627
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,...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.