473,327 Members | 1,967 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,327 software developers and data experts.

Functions with pointers in c#.net

I am trying to communicate with an outside instrument using a .dll. I am new to working with dll's and am not sure how to do this. The function calls for the following parameters which I kind of think I am doing right:

[DllImport("MNW.dll", EntryPoint = "mnPollDevices")]
public static extern unsafe int mnPollDevices([MarshalAs(UnmanagedType.LPArray)] int*[] device_flag,
int* port,
int* buffer,
[MarshalAs(UnmanagedType.LPArray)] char*[] message,
int * devicesFound);

The function should pass an int array of size 128 into the function and when it comes out, the array should contain some values. My problem is actually calling the function. I have tried every combination of pointers and no pointers being sent to the function but am not able to compile. Does anyone know the right syntax? Hardware is the class containing the function. Here is what I am currently using (I know they are not pointers):

int[] flag = new int[128];
int port = 0; //COM1 is 0
int buffer = 128;
int device_Count = 0;
char[] message = new char*[80];
int returncode = 0;

returncode = Hardware.mnPollDevices(flag, port, buffer, message, device_Count);
Jun 26 '08 #1
3 1494
Plater
7,872 Expert 4TB
Well I think for char* you can just pass in a string?
You probably also need to pass by reference (see the ref parameter keyword)
What are the complie errors?
Jun 26 '08 #2
I was actually able to make this work using the ref command, but now I am getting an exception saying the following:

The runtime has encountered a fatal error. The address of the error was at 0x79e71bd7, on thread 0x510. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

I think it has to do with passing the int/char array. I have no idea what the marshall is (I found some code segments online that use this nomenclature for passing arrays to and from .dll's in c#).

Here is the segment I am calling:
Expand|Select|Wrap|Line Numbers
  1.  [DllImport("MNW.dll", EntryPoint = "mnPollDevices")]
  2. public static extern int mnPollDevices([MarshalAs(UnmanagedType.LPArray)]ref int[] device_flag,
  3.  
  4.                                                 ref int port,
  5.                                                 ref int buffer,
  6.                                                 [MarshalAs(UnmanagedType.LPArray)] ref char[] message,
  7.                                                 ref int devicesFound);     
  8.  
And the following is the call:
Expand|Select|Wrap|Line Numbers
  1.                 int[] flag = new int[128];
  2.                 int port = 0; //COM1 is 0
  3.                 int buffer = 128;
  4.                 int device_Count = 0;
  5.                 char[] message = new char[80];
  6.                 int returncode = 0;
  7.                 int BaudRate = 5;
  8.                 try
  9.                 {                   
  10.                     //INIT PORT                    
  11.                    returncode = Hardware.mnInitComm(ref port, ref BaudRate, ref buffer); //5 is baudrate 9600
  12.                    returncode = Hardware.mnSetBaud(ref port, ref BaudRate);
  13.                    returncode = Hardware.mnPollDevices(ref flag,ref port,ref buffer, ref message,ref device_Count);
  14.                 }
  15.                 catch(Exception ex) {
  16.                     MessageBox.Show(ex.ToString());
  17.                 }  
  18.  
Has anybody ever ran into this before?
Jun 26 '08 #3
OMG! I am a moron!

I found the answer.
Arrays are already passed by referance in C#, so I don't need to pass by referance when I use it.

Thank you.
Jun 26 '08 #4

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

Similar topics

12
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
5
by: Steve | last post by:
Can anyone tell me if I can have an array of functions that take a variable number of parameters? If it is possible I'd like to know how to declare the array and the functions as its elements. I am...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
15
by: Christian Christmann | last post by:
Hi, in which situations pointers to functions might be more efficient/convenient than a direct function call? In the example I've found so far, I see no advantage of using pointers to...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: 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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.