473,789 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PInvoke query

I've a problem with PInvoke.I've to invoke a unmanaged dll
fucntion in C# which uses a callback fucntion.Please
apologise for this lenghty message and render ur kind help.
The original dll function and my C# code has been given
below along with the error message.

Error: The return code is 0 which means the fucntion has
executed successfully. But I'm not getting the correct
values in the structure pborcadirentry.
Note: I've tried GC method. But I'm not sure whether I've
applied it appropriately.I 've also tried [MarshalAs
(UnmanagedType. Struct)] and [MarshalAs
(UnmanagedType. LPStruct)] for marshaling strcuture ans
class.
But there was no change in the o/p. I feel that there is
some problem with the memory. But I'm unable to detect it.
Please help me


The unmanaged dll fucntion is as follows

*************** *************** **********
The Original Fucntion in the dll
*************** *************** **********
int PBORCA_LibraryD irectory ( HPBORCA hORCASession, LPSTR
lpszLibName, LPSTR lpszLibComments , int iCmntsBuffSize,
PBORCA_LISTPROC pListProc, LPVOID pUserData );

hORCASession-Handle to previously established ORCA session

pListProc- Pointer to the PBORCA_LibraryD irectory callback
function.

pUserData -Pointer to user data to be passed to the
PBORCA_LibraryD irectory callback function

Given below function is the callback function declaration
in C.

typedef void (FAR PASCAL *PBORCA_LISTPRO C) (
PPBORCA_DIRENTR Y, LPVOID );

PPBORCA_DIRENTR Y -Pointer to the structure
PBORCA_DIRENTRY , described next
LPVOID -Long pointer to user data

The user data strcuture to the callback fucntion is as
follows:

typedef struct ORCA_UserDataIn fo
{
LPSTR lpszBuffer; // Buffer for entry info
DWORD dwCallCount; // # of entries in lib
DWORD dwBufferSize; // size of buffer
DWORD dwBufferOffset; // current offset in buffer
} ORCA_USERDATAIN FO, FAR *PORCA_USERDATA INFO;

The structure passed to the callback fucntion by the
LibraryDirector y fucntion is as follows:

typedef struct pborca_direntry { CHAR szComments
[PBORCA_MAXCOMME NT + 1];
LONG lCreateTime;
LONG lEntrySize;
LPSTR lpszEntryName;
PBORCA_TYPE otEntryType;} PBORCA_DIRENTRY , FAR
*PPBORCA_DIRENT RY;

Note:PBORCA_TYP E is an enum

*************** *************** *********
End of the dll fucntion
*************** *************** *********

_______________ _______________ ___________
C# code
_______________ _______________ ___________

[StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Auto)]
public class UserData
{
public string s;
public uint callcount;
public uint bufsize;
public uint offset;
}
[StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Auto)]
public struct pborca_direntry
{
public const int i=255;
[MarshalAs
(UnmanagedType. ByValTStr,SizeC onst=i+1)]
public string s;
public System.Int32 time;
public System.Int32 size;
[MarshalAs(Unman agedType.LPStr)]
public string name;
public int type;
}
public unsafe delegate void Callback(ref pborca_direntry
p,UserData usr);

[DllImport
("pborc80.dll", CharSet=CharSet .Auto,EntryPoin t="PBORCA_Lib r
aryDirectory",C allingConventio n=CallingConven tion.StdCall)]
//...Ansi)]
public static extern int LibraryEntry(in t hnd,[MarshalAs
(UnmanagedType. LPStr)]System.String s,[Out]char[] buff,int
se,[MarshalAs(Unman agedType.Functi onPtr)]Callback
hr,UserData usr);


public class1
{
Callback c = new Callback(Class1 .libdircallback );

GC.KeepAlive(co de=class3.Libra ryEntry
(hnd,"d:\\proje cts\\current\\t gt3.pbl",st,100 00,c,usr));

public unsafe static void libdircallback( ref
pborca_direntry s,UserData usr) {
{
Console.WriteLi ne("hai,I'm in
callback");
Console.WriteLi ne
(s.type);
}

}
Thanks in advance,
Vijaya
Nov 22 '05 #1
0 1299

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

Similar topics

4
3717
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.
0
338
by: vijaya | last post by:
I've a problem with PInvoke.I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.Please apologise for this lenghty message and render ur kind help. The original dll function and my C# code has been given below along with the error message. Error: The return code is 0 which means the fucntion has executed successfully. But I'm not getting the correct values in the structure pborcadirentry. Note: I've tried GC...
5
2523
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to execute a function of a unmanaged dll using PInvoke, i have definied the function as: public static extern int isc_dsql_prepare( int status_vector, ref int trans_handle, ref int stmt_handle,
2
2994
by: Jeff Lederer | last post by:
I have created a simple test C# console program that calls an unmanaged C subroutine in a DLL where one of the arguments is a callback to the C# code. I noticed that when the callback has no paramaters, it works fine. But when there are parameters, I get the exception: "Runtime failure check #0: The value of ESP not properly saved..." Here are the facts: The delegate declaration: >>> public delegate int querydlg(IntPtr pEnv, string...
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
5
7886
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
6659
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
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...
14
3797
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain more why C++/CLI would be better to PInvoke than doing the PInvoke in C#? Because, usually in C# as you already know we use DLLImport and extern
0
9666
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
9511
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
10408
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...
1
10139
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
9983
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
9020
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...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
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

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.