473,663 Members | 2,864 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(v ar Path:pansichar;
var DataLenth:integ er):integer;std call;

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(5 12);

[DllImport("OziA PI.dll", CharSet = CharSet.Ansi, CallingConventi on =
CallingConventi on.StdCall)]

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

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

ret=oziGetExePa th(ref sb, ref size);
//if (ret != 0)
// throw new Exception("Coul d 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.PtrToS tringAuto()" 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 3886
Id try again with IntPtr
Did you try Marshal.PtrToSt ringAnsi (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(v ar Path:pansichar;
var DataLenth:integ er):integer;std call;

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(5 12);

[DllImport("OziA PI.dll", CharSet = CharSet.Ansi, CallingConventi on =
CallingConventi on.StdCall)]

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

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

ret=oziGetExePa th(ref sb, ref size);
//if (ret != 0)
// throw new Exception("Coul d 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.PtrToS tringAuto()" 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******@discu ssions.microsof t.com> wrote in message news:<75******* *************** ************@mi crosoft.com>...
Id try again with IntPtr
Did you try Marshal.PtrToSt ringAnsi (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
2834
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 dwIoControlCode, ref int lpInBuffer,
1
1101
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
10681
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 operation, and I have to use it from my C# program. I am really unsure what type of marshalling I should use. The data parameter is a pointer to an array of byte data. This data must not be copied when calling the function. Plus, the .NET...
2
5782
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 supported by C#. In this case if I am not mistaken the system performs default marshalling - matching the data types to its most similar equivalent in C#. Is this right? Also, I presume this can be overridden by using MarshalAs. Is this
1
1635
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 takes a "filename" as one of its arguments and the type of that is "char*". In the documents of msdn, "char*" is mapped to String or StringBuilder by marshaller. although i searched in the goggle, i couldnt find any kinds of simple-because I am not...
1
4674
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 have a methods with signitures like the following... unsigned char someMethod(unsigned char blah, SOMESTRUCT* somestruct);
2
1276
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 you? I have seen from several posts that you have to do special conversions for strings, but what about the other types such as int, long,ect. In my case I want to save it as a member variable from a managed class into an unmanaged class's.
3
6312
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 that no matter if we pass TRUE or FALSE, the bool is always marshalled as true. // unmanaged code in dll typedef bool (__stdcall *BoolCallBack)(short b);
11
2695
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 this: public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
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
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8548
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.