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

Pointers in .net

178 100+
Hello everyone,


I have just started a project that requires a windows service that read output from a PCI card via an API. I've been unfortunate enough to be handed an ActiveX .ocx library that exposes methods to use, but with no documentation specific to using the methods available in a .NET environment (last API / driver update was Nov 02).


The method I need looks likes this - void MultiRead(short channel1, short channel2, ref float pDataBuffer).


As you can probably tell, the problem lies with the 3rd parameter, which is a pointer to a "one dimensional array." When I try to create a pointer to a byte[] I get this compilation error


"Cannot take the address of, get the size of, or declare a pointer to a managed type."



I know its a long shot , but has anyone had a problem like this before, and can you help me out? I need to know how I can create a one dimensional array capable of being populated with text or byte data, and which is also an object or struct that I can create a pointer to.
Apr 9 '09 #1
6 3130
cloud255
427 Expert 256MB
Hi

It is possible to do this. You need to mark the function which calls to the API as Unsafe and use the pointers within a Fixed block.

Using the Unsafe keyword in your function declaration allows you to create pointers in C#.
The Fixed keyword tells the garbage collector to leave to pointers alone until code execution has exited the fixed block.

So basically all you need to do is create your function using the Unsafe keyword. Create a 'Fixed' block, write your code around pointers there and everything should work OK.
Apr 9 '09 #2
cleary1981
178 100+
Hi,

Thanks for the reply. I am aware of what you have, but the problem is that I need to create a pointer to a 1 dimensional array which i'm struggling to do. I tried to point to a byte[] but I get a compliation error::



"Cannot take the address of, get the size of, or declare a pointer to a managed type."

I need to know how I can create a one dimensional array capable of being populated with text or byte data, and which is also an object or struct that I can create a pointer to. Any idea how to do this?
Apr 9 '09 #3
cloud255
427 Expert 256MB
I'm not sure what problem you're having.
Could you maybe post the code which is giving you problems.

In your application properties under 'Build' did you allow unsafe code?

I wrote a little test which worked fine:

Expand|Select|Wrap|Line Numbers
  1.  private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             byte[] x = new byte[] {1,2,3,4,5};
  4.             test(x);
  5.         }
  6.  
  7.  private unsafe void test(byte[] src)
  8.         {
  9.             fixed (byte* pSrc = src)
  10.             {
  11.                 MessageBox.Show("First element: " +  pSrc[0].ToString() +  " Length of array: " + src.Length.ToString());
  12.             }
  13.         }
Apr 9 '09 #4
cleary1981
178 100+
You have not assigned your pointer value using &src, which means you are not pointing to the memory address of the array, if I am not mistaken. You will not recieve a compilation error when not using &src, but I think this means your not actually pointing to the address in memory

The problem is the method signature is this::

Expand|Select|Wrap|Line Numbers
  1. void MultiRead(short channel1, short channel2, ref float pDataBuffer);
  2.  
Where pDataBuffer is a float pointer to the buffer (byte array). The documentation on this method doesn't help much, which is as follows:

Description :

Perform multiple channel AD conversions by software polling

Syntax

Sub Object.MultiRead(ByVal StartChannelNo as Integer,

ByVal EndChannelNo,ByRef ADbuffer as single)


Return : none


Argument :


StartChannelNo :Start Channel number of analog input (0~15)

EndChannelNo :Stop Channel number of analog input (0~15)

ADBuffer :Points to one-Dimension array with the size

of EndChannelNo-StartChannelNo+1


Any ideas ??
Apr 9 '09 #5
cloud255
427 Expert 256MB
I changed the code as follows:

Expand|Select|Wrap|Line Numbers
  1.    private unsafe void test(byte[] src)
  2.         {
  3.             fixed (byte* pSrc = src)
  4.             {
  5.                 for (int i = 0; i < 5; i++)
  6.                 {
  7.                     pSrc[i] = 0;
  8.                     MessageBox.Show("Element number " + i.ToString() + " = " + src[i].ToString());
  9.                 }
  10.             }
  11.         }
This code shows that all the elements inside the src array now have a value of 0, so the pointer is pointing to the correct location as the data has changed.


I get the same results when changing the arguments to:

Expand|Select|Wrap|Line Numbers
  1. private unsafe void test(ref byte[] src)
I hope this helps you.. I'm running out of ideas :)
Apr 9 '09 #6
Plater
7,872 Expert 4TB
You might want to use marshalling for this?

Not sure for sure how to use it, just pulled this from another post here on bytes, but play around with something like:
Expand|Select|Wrap|Line Numbers
  1. int len=4;//just guessing at this
  2. IntPtr data = Marshal.AllocHGlobal(len); 
  3. byte a;
  4. byte b;
  5.  
  6. try 
  7. {
  8.     MultiRead(a, a, data).
  9.  
  10.     byte[] returnBytes = new byte[len]; 
  11.     Marshal.Copy(data, returnBytes, 0, len); 
  12.  
  13.     Marshal.FreeHGlobal(data); 
  14. }
  15.  
Apr 9 '09 #7

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

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
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...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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,...

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.