473,765 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DllImport problem

2 New Member
Hey All,


How do I declare a following types:

Expand|Select|Wrap|Line Numbers
  1.  char** 
  2.  unsigned char**
  3.  
in C# DllImport function?

C++ function signature
Expand|Select|Wrap|Line Numbers
  1. -----------------------------------------------------------------------------------------------------------
  2. extern "C" __declspec(dllimport) int Get
  3. (int*     pEnrollNumber,                                                          
  4.  char** EnrollNames,                                                          
  5.  int*     EnrollIDs,                                                         
  6.  unsigned char** EnrollTemplates,
  7.  int* EnrollTemplateSizes,                                                       
  8.  int Delay);
  9. -----------------------------------------------------------------------------------------------------------
  10.  
I tried to use an IntPtr instead of both char** and unsigned char**,
but it throws an AccessViolation Exception.

If I declare a char** (for char**) and byte** (instead of unsigned char**) I still get the same exception but it seems to me the problem in this case is because of
parameters i pass to the function(i'm not quite sure how to declare it..)



C# declarations i tried:
Expand|Select|Wrap|Line Numbers
  1.  [DllImport("TLib.dll")]
  2.         private static extern int EnrollGet
  3. ( ref int pEnrollNumber, char** EnrollNames,
  4.   ref int EnrollIDs, 
  5.   byte** EnrollTemplates,
  6.   ref int EnrollTemplateSizes,
  7.   int Delay);
  8.  
  9. or
  10.  
  11.  [DllImport("TLib.dll")]
  12.         private static extern int EnrollGet
  13. ( ref int pEnrollNumber, 
  14.  out IntPtr EnrollNames,
  15.  ref int EnrollIDs, 
  16.  out IntPtr EnrollTemplates,
  17.  ref int EnrollTemplateSizes,
  18. int Delay);
  19.  
Can anyone please help me to solve this issue?

Thanks in advance
Apr 21 '09 #1
1 1719
mrssuper
2 New Member
I finally solved the problem.

I left the declaration as it is in C++:

[DllImport("TLib .dll")]
public static extern int Get( int* pEnrollNumber,c har** EnrollNames,int * EnrollIDs,
byte** EnrollTemplates ,int* EnrollTemplateS izes, int Delay);

Turns out that all integers point also to arrays so I created the following class:


unsafe class CUser
{
public int m_Number;
public int m_Index;
public char*[] m_Names;
public int[] m_IDs;
public int[] m_TemplateSizes ;
public byte*[] m_Templates;

public CUser()
{

// 1000 = db size

m_Number = 0;
m_Index = 0;
m_Names = new char*[1000];
m_IDs = new int[1000];
m_TemplateSizes = new int[1000];
m_Templates = new byte*[1000];

for (int i = 0; i < 1000; ++i)
{
m_Names[i] = (char*)Memory.A lloc(64);
m_Templates[i] = (byte*)Memory.A lloc(1000);
}
}

~CUser()
{


for (int i = 0; i < MAX_USER_ENTRIE S; ++i)
{
Memory.Free(m_T emplates[i]);
}
}
}

The trick is to allocate the memory for both the array of strings and bytes and pass a correct parameters which I didn't before.
I did it using the microsoft sample of dinamic Memory allocation.
The Class found in: http://msdn.microsoft.com/en-us/libr...86(VS.71).aspx


The function call:




unsafe
{

CUser users = new CUser();




fixed (int* num = &users.m_Number , ids = &users.m_IDs[0], sizes = &users.m_Templa teSizes[0])
{
fixed (byte** temps = &(users.m_Templ ates[0]))
{
fixed (char** names = &(users.m_Na mes[0]))
{
int result = Helper.Get(num, names, ids, temps, sizes);
}
}
}
}

Maybe it a little bit difficult, but it works..
Apr 21 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2408
by: caviar | last post by:
Hi, i'm having a problem with a native w32 dll. Everything is working fine except for one parameter which is defined as: --------------------------------------header file--- // short PASFIX PAF_GetRecord(short listndx, long recno,char *pafrec, short pafreclen ); I'm having problems with the char *pafrec param, i translated (after many/many/many) into this below, after trying several other usenet posting
15
4468
by: Jim | last post by:
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the windows console the application runs flawlessly, but once I call the class from inside a c# windows application the program freezes, crashes etc... there must be a way to make this work inside a windows app as it run great as a command line app.
2
5308
by: Kurt Ng | last post by:
Hi, y'all. Can anyone help me on this problem? I'm working with a third-party C dll, and I'm having trouble importing into C# the dll's methods that return one of the dll's defined types, which are all defined as opaque pointers. What I tried to do is use IntPtr for the opaque pointer return type, but there seems to be a resulting signature problem.
1
4352
by: Whidbey Wave | last post by:
Hi , Of course sometimes, some of us have these weird DLLImport issues. I am using Net-snmp library from http://www.net-snmp.org. While creating a prototype as console application it worked fine, but as windows application it fails to respond correctly. Verified by sniffer, the windows app does not even reaches TCP/IP network layer, and it returns no error. The function which I imported from this library are: init_snmp, snmp_set and...
2
10483
by: Brian Anderson | last post by:
Hello, is it possible to use DllImport to call a DLL in ASP.NET ? Or is it necessarry that my DLL has to be copied into \System32 ? My DLL is a native C++ 7.1 DLL (not managed, no COM, no regsvr32) and uses Assembler to make some math in SSE. The result is given out as an Int. Using a C# console app (code below) it work fine & fast.
1
2085
by: Seth Gecko | last post by:
Hi I am developing a complex VB.Net Windows application for an engineering firm (don't ask me why they prefer VB.Net...). All the engineering calculations are done in FORTRAN which is compiled to a non COM type DLL (meaning you can't create an Interop for it). This is called from within the .Net application using DllImport, just like using a Windows API call. This works fine. The problem arises when we create threads (using BeginInvoke)...
1
4281
by: kardon33 | last post by:
Let me explain my problem, Im currently trying to use a Perl module that was built for a Windows OS that uses a .dll and .lib file. I have obtained the c header files that the modules were built with. I am trying to use SWIG to write my Perl mod with those files but I getting a bunch of errors with the "extern "C" __declspec(dllimport)" command kinda know that basics of the command, its used to declare vars from a .dll. If im wrong please...
1
2771
by: JohnCox | last post by:
I have a simple Win32 DLL I wrote named "SimpleLib" that exports two functions. It is written in C++ and compiled with __stdcall (/Gz) and with the preprocessor definition _MBCS (not Unicode). The first function is called "StrFirst" and takes in a LPTSTR as the first parameter and a long as the second, like this: SIMPLELIB_API int StrFirst(LPTSTR str, long num); The second function is essentially the same thing, but with the order of the...
1
5797
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some code: #define USERINT_FUNC __cdecl #ifdef __cplusplus extern "C" { #endif
0
9568
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
10168
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
9959
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
9837
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...
1
7381
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
6651
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
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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.