473,326 Members | 2,168 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,326 software developers and data experts.

Callbacks of unmanaged code

I've to invoke a unmanaged dll fucntion in C# which uses
a callback fucntion.The unmanaged dll fucntion is as
follows

****************************************
The Original Fucntion in the dll
****************************************
int PBORCA_LibraryDirectory ( 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_LibraryDirectory callback
function.

pUserData -Pointer to user data to be passed to the
PBORCA_LibraryDirectory callback function

Given below function is the callback function declaration
in C.

typedef void (FAR PASCAL *PBORCA_LISTPROC) (
PPBORCA_DIRENTRY, LPVOID );

PPBORCA_DIRENTRY -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_UserDataInfo
{
LPSTR lpszBuffer; // Buffer for entry info
DWORD dwCallCount; // # of entries in lib
DWORD dwBufferSize; // size of buffer
DWORD dwBufferOffset; // current offset in buffer
} ORCA_USERDATAINFO, FAR *PORCA_USERDATAINFO;

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

typedef struct pborca_direntry { CHAR szComments
[PBORCA_MAXCOMMENT + 1];
LONG lCreateTime;
LONG lEntrySize;
LPSTR lpszEntryName;
PBORCA_TYPE otEntryType;} PBORCA_DIRENTRY, FAR
*PPBORCA_DIRENTRY;

Note:PBORCA_TYPE is an enum
PBORCA_MAXCOMMENT=255
***************************************
End of the dll fucntion
***************************************

_________________________________________
C# code which I've written
_________________________________________

[StructLayout(LayoutKind.Sequential,CharSet=CharSet .Auto)]
public class UserData
{
public string s;
public uint callcount;
public uint bufsize;
public uint offset;
}
[StructLayout(LayoutKind.Sequential,CharSet=CharSet .Auto)]
public struct pborca_direntry
{
public const int i=255;
[MarshalAsUnmanagedType.ByValTStr,SizeConst=i+1)]
public string s;
public System.Int32 time;
public System.Int32 size;
[MarshalAs(UnmanagedType.LPStr)]
public string name;
public int type;
}
public unsafe delegate void Callback(ref pborca_direntry
p,UserData usr);

[DllImport
("pborc80.dll",CharSet=CharSet.Auto,EntryPoint="PB ORCA_Libr
aryDirectory")]
public static extern int LibraryEntry(int hnd,[MarshalAs
(UnmanagedType.LPStr)]System.String s,[Out]char[] buff,int
se,[MarshalAs(UnmanagedType.FunctionPtr)]Callback
hr,UserData usr);


public class1
{
Callback c = new Callback(Class1.libdircallback);
char[] st = new char[10000];
GC.KeepAlive(code=class3.LibraryEntry
(hnd,"d:\\projects\\current\\tgt3.pbl",st,10000,c, usr));

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

}

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 structure and
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

Thanks in advance,
Vijaya
Jul 21 '05 #1
0 1120

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

Similar topics

1
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those...
1
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those...
23
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have...
4
by: R. MacDonald | last post by:
Hello, all, I have a .NET application (VB) that passes the address of a delegate to unmanaged code in a DLL. The unmanaged code then uses the delegate as a call-back. This seems to work...
2
by: cada0310 | last post by:
Hi there, I'm new to C# (but not to C++), and I'm trying to create a web service that calls some of our older software, located in a DLL. I'm getting most the calls to work ok, but one of them...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
3
by: kelvin.koogan | last post by:
I have an unmanaged DLL which calls my managed C++ app using a callback (standard C++ function pointer). I can receive the callbacks OK using a static function with global scope, but I can't find a...
8
by: DavidT | last post by:
Hello, at first, exuse if the following question is simple to solve, but i normaly coding with C# and now have to use C++/CLI for one project. My Problem is that i have to use a native c++ sdk...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.