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);
3 1494
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?
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: -
[DllImport("MNW.dll", EntryPoint = "mnPollDevices")]
-
public static extern int mnPollDevices([MarshalAs(UnmanagedType.LPArray)]ref int[] device_flag,
-
-
ref int port,
-
ref int buffer,
-
[MarshalAs(UnmanagedType.LPArray)] ref char[] message,
-
ref int devicesFound);
-
And the following is the call: -
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;
-
int BaudRate = 5;
-
try
-
{
-
//INIT PORT
-
returncode = Hardware.mnInitComm(ref port, ref BaudRate, ref buffer); //5 is baudrate 9600
-
returncode = Hardware.mnSetBaud(ref port, ref BaudRate);
-
returncode = Hardware.mnPollDevices(ref flag,ref port,ref buffer, ref message,ref device_Count);
-
}
-
catch(Exception ex) {
-
MessageBox.Show(ex.ToString());
-
}
-
Has anybody ever ran into this before?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
| |