473,803 Members | 3,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

More PInvoke Shenanigans

A couple of days ago I was debating about whether to use C++/CLI or C#
2005's PInvoke in order to tie in my VS 2005 app with a legacy
csta32.dll file. So far I have tried to call one method out of the
csta32.dll and wind up with the PInvoke stack imbalance error. Here
are the data types my C# mappings are based on:

typedef unsigned long ACSHandle_t;

typedef enum {
APP_GEN_ID, /* application will provide invokeIDs; any 4-byte value
is legal */
LIB_GEN_ID /* library will generate invokeIDs in the range 1-32767
*/
} InvokeIDType_t;

typedef unsigned long InvokeID_t;

typedef enum StreamType_t {
ST_CSTA = 1,
ST_OAM = 2,
ST_DIRECTORY = 3,
ST_NMSRV = 4
} StreamType_t;

typedef char ServerID_t[49];
typedef char LoginID_t[49];
typedef char Passwd_t[49];
typedef char AppName_t[21];

typedef enum Level_t {
ACS_LEVEL1 = 1,
ACS_LEVEL2 = 2,
ACS_LEVEL3 = 3,
ACS_LEVEL4 = 5
} Level_t;

typedef char Version_t[21];

typedef unsigned short SendQSize_t;
typedef unsigned short SendExtraBufs_t ;
typedef unsigned short RecvQSize_t;
typedef unsigned short RecvExtraBufs_t ;

typedef struct PrivateData_t {
char vendor[32];
unsigned short length;
char data[1]; /* actual length determined by application */
} PrivateData_t;

And here is what I have so far in C# to translate between the managed
and unmanaged elements:

[DllImport("csta 32.dll")]
public static extern int acsOpenStream(r ef IntPtr acsHandle, int
invokeIDType, ulong invokeID, int streamType,
ref string serverID, ref string loginID, ref string passwd, ref
string applicationName , int acsLevelReq,
ref string apiVer, ushort sendQSize, ushort sendExtraBufs, ushort
recvQSize, ushort recvExtraBufs,
ref PrivateData_t priv);

public struct PrivateData_t
{
[ MarshalAs( UnmanagedType.B yValArray, SizeConst=32)]
public char [] vendor;
public ushort length;
[ MarshalAs( UnmanagedType.B yValArray, SizeConst=1)]
public char [] data;
};

The acsHandle is derived by the following client routine when I invoke
the acsOpenStream() method in C#:

ulong acsHandle = 0;
IntPtr ptrAcsHandle = (IntPtr)acsHand le;

Am I missing something obvious here? This all seems relatively complex
to me and I'm struggling with getting this first method knocked out. I
read that in C++ the long data type is 4 bytes, while in C# it's 8
bytes. Is this causing the hangup?

May 20 '07 #1
1 3046
On May 20, 2:41 pm, gregarican <greg.kuj...@gm ail.comwrote:
A couple of days ago I was debating about whether to use C++/CLI or C#
2005's PInvoke in order to tie in my VS 2005 app with a legacy
csta32.dll file. So far I have tried to call one method out of the
csta32.dll and wind up with the PInvoke stack imbalance error. Here
are the data types my C# mappings are based on:

typedef unsigned long ACSHandle_t;

typedef enum {
APP_GEN_ID, /* application will provide invokeIDs; any 4-byte value
is legal */
LIB_GEN_ID /* library will generate invokeIDs in the range 1-32767
*/

} InvokeIDType_t;

typedef unsigned long InvokeID_t;

typedef enum StreamType_t {
ST_CSTA = 1,
ST_OAM = 2,
ST_DIRECTORY = 3,
ST_NMSRV = 4

} StreamType_t;

typedef char ServerID_t[49];
typedef char LoginID_t[49];
typedef char Passwd_t[49];
typedef char AppName_t[21];

typedef enum Level_t {
ACS_LEVEL1 = 1,
ACS_LEVEL2 = 2,
ACS_LEVEL3 = 3,
ACS_LEVEL4 = 5

} Level_t;

typedef char Version_t[21];

typedef unsigned short SendQSize_t;
typedef unsigned short SendExtraBufs_t ;
typedef unsigned short RecvQSize_t;
typedef unsigned short RecvExtraBufs_t ;

typedef struct PrivateData_t {
char vendor[32];
unsigned short length;
char data[1]; /* actual length determined by application */

} PrivateData_t;

And here is what I have so far in C# to translate between the managed
and unmanaged elements:

[DllImport("csta 32.dll")]
public static extern int acsOpenStream(r ef IntPtr acsHandle, int
invokeIDType, ulong invokeID, int streamType,
ref string serverID, ref string loginID, ref string passwd, ref
string applicationName , int acsLevelReq,
ref string apiVer, ushort sendQSize, ushort sendExtraBufs, ushort
recvQSize, ushort recvExtraBufs,
ref PrivateData_t priv);

public struct PrivateData_t
{
[ MarshalAs( UnmanagedType.B yValArray, SizeConst=32)]
public char [] vendor;
public ushort length;
[ MarshalAs( UnmanagedType.B yValArray, SizeConst=1)]
public char [] data;

};

The acsHandle is derived by the following client routine when I invoke
the acsOpenStream() method in C#:

ulong acsHandle = 0;
IntPtr ptrAcsHandle = (IntPtr)acsHand le;

Am I missing something obvious here? This all seems relatively complex
to me and I'm struggling with getting this first method knocked out. I
read that in C++ the long data type is 4 bytes, while in C# it's 8
bytes. Is this causing the hangup?
Just after posting this I saw that I needed to change the C# ulong to
System.UInt32. Now I get past that error and can at least get a result
code back from the csta32.dll. So I am on my way!!!

May 20 '07 #2

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

Similar topics

4
3718
by: Ted | last post by:
Is it possible to use mailslots in .NET using PInvoke? I have a VC++ 6.0 based app that creates and listens to a mailslot. I have a second VC++ 6.0 based app that opens the mailslot and writes to it successfully. So this stuff works. Additionally, I have a VB.NET app that opens the mailslot successfully using CreateFile() (with PInvoke). What looks like a proper handle is returned and there is no error.
3
1891
by: Ted Miller | last post by:
Hi folks, I've got an unmanaged routine I'm pinvoking to. It takes a pointer to an array of 3 pointers to bytes, typed as byte**. public static extern void foo(byte **p3pb); unsafe { byte pB = new byte;
3
3470
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is that my form appears to be blocked while the background thread is running. I am very familir with threads in unmanaged code but am just getting into them in C#. Are there issues I need to be aware of with regards to threading a simple pinvoke...
5
2830
by: vertigo | last post by:
Hello I use some win 32 API function for example: HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile
2
3341
by: Craig | last post by:
I've seen many examples of how to call SHGetFileInfo in shell32.dll to get a files associated icon, but I can't find anywhere how to get the file information (size, last date modified, etc, etc) out of shell32 using pinvoke. I have however seen how to get this information using COM: Shell32.Shell shell = new Shell32.ShellClass(); Shell32.Folder folder = shell.NameSpace("C:\\"); Shell32.FolderItem folderItem =...
1
1867
by: cppdev | last post by:
Hello, After reading a few articles, http://blogs.gotdotnet.com/cbrumme/PermaLink.aspx/e55664b4-6471-48b9-b360-f0fa27ab6cc0 http://blogs.gotdotnet.com/anathan/commentview.aspx/8ec0b7b2-6290-4500-a8c7-1b6c677214cb i have the following question: __gc class C {
5
7887
by: _iycrd | last post by:
After numerous problems, I'm having second thoughts about using C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like it will take a huge amount of work, if it will work at all. The native DLL uses raw data buffers that are normally handed to it (via pointer) from another native function. If I'm piloting this with C#/PInvoke, how could those raw data buffers be created and immobilized? In C++/CLI, there is pin_ptr to...
8
6661
by: Rajesh Soni | last post by:
Hi! I'm getting a PInvoke error while trying to execute the following code... declaration: Structure POINTAPI Dim x As IntPtr
0
339
by: gregarican | last post by:
A couple of days ago I was debating about whether to use C++/CLI or C# 2005's PInvoke in order to tie in my VS 2005 app with a legacy csta32.dll file. So far I have tried to call one method out of the csta32.dll and wind up with the PInvoke stack imbalance error. Here are the data types my C# mappings are based on: typedef unsigned long ACSHandle_t; typedef enum { APP_GEN_ID, /* application will provide invokeIDs; any 4-byte value
0
1201
by: Benosham | last post by:
I have been playing around with trying to PInvoke GDI+ from C#, I made recently made the transition from C/C++ to C# and I really like the language, however being the old fashioned programmer I am I don't like the idea of the framework slowing down my applications with uncessesary safety checks. So I decided to try and PInvoke into GDI+ directly so tweak some extra performance out of an application I am writing. But every time I try I get an...
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
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,...
0
10548
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
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
10069
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
9125
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
4275
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 we have to send another system
2
3798
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.