Pointer's in .NET RRS feed

  • Question

  • 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.


    • Edited by sean_mufc Wednesday, April 8, 2009 5:10 PM title change
    Wednesday, April 8, 2009 5:09 PM

All replies

  • You can use Marshal.Alloc / Marshal.Copy to pass byte arrays from (to) unmanaged code.
    Wednesday, April 8, 2009 6:20 PM
  • H,

    dumpbin/exports returns the following. I am not sure what I should be looking for here, can you help me out? I should also mention i need to use c# to call these functions not c++.

    File Type: DLL

      Section contains the following exports for PCX2026.OCX

        00000000 characteristics
        3F8A09D5 time date stamp Mon Oct 13 04:11:33 2003
            0.00 version
               1 ordinal base
               4 number of functions
               4 number of names

        ordinal hint RVA      name

              1    0 00002CB0 DllCanUnloadNow
              2    1 00002C82 DllGetClassObject
              3    2 000010B0 DllRegisterServer
              4    3 00001130 DllUnregisterServer


            2000 .data
            2000 .rdata
            1000 .reloc
            2000 .rsrc
            2000 .text

    Wednesday, April 8, 2009 7:05 PM
  • Hi,

    I need to pass a pointer to the array not the actual array.  
    Wednesday, April 8, 2009 7:08 PM
  • Since you're using COM interop, and not pInvoke, things are a bit different.

    It sounds like your current COM interop assembly is not specified correctly.  You will probably need to rework it to marshall the data across as a byte[] or float[].

    For details and more information on creating your own COM interop layer, check this MSDN section:
    • Marked as answer by Zhi-Xin Ye Wednesday, April 15, 2009 7:41 AM
    • Unmarked as answer by sean_mufc Thursday, April 16, 2009 7:02 AM
    Wednesday, April 8, 2009 7:25 PM
  • Since you have an ActiveX control you should try to get the type library - try to open the ocx with OleView and if it has an embedded typelib you will see it. Is the method you're having problems with using a SAFEARRAY ?

    For functions using a bare "byte*" you can do the following:

            public static extern void SomeFunction(IntPtr byteBuffer);
            static void Main(string[] args)
                int buffSize = 1024;
                IntPtr buf = Marshal.AllocHGlobal(buffSize);
                //call function
                byte[] data = new byte[buffSize];
                Marshal.Copy(buf, data, 0, buffSize);

    Wednesday, April 8, 2009 7:34 PM