I'm trying to rewrite "c" code to "c#" i'm stuck in some point.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shirahama Kenichi
    New Member
    • Jun 2010
    • 3

    I'm trying to rewrite "c" code to "c#" i'm stuck in some point.

    Hi...
    I'm new to c# and got some problems in rewriting "C" code in "C#".
    I got this error message "You can only take the address of an unfixed expression inside of a fixed statement initializer" and i'm not sure whats wrong??? can anybody light my path?
    here's the "C" code:
    Code:
    USHORT APIENTRY16 PrtOpenDevice (HFILE * _Seg16 PrtHandle, USHORT PrtDriver);
    HANDLE PrtHandle;       /* Printer handle */
    USHORT PrtDriver;       /* Logical device driver */
    USHORT rc;              /* Return code */
    
    PrtDriver = 1;          /* 1st logical device driver */
    
    rc = PrtOpenDevice (&PrtHandle, PrtDriver);
    and this is what i have done so far:
    Code:
    unsafe public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImportAttribute("WNTDFPRT.DLL", EntryPoint = "PrtOpenDevice")]
        public static extern ushort PrtOpenDevice([System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.U8)] long *PrtHandle, ushort pPort);
    
        public long PrtHandle = 0;
    
        public Form1()
        {
            InitializeComponent();
            ushort rc = PrtOpenDevice(&PrtHandle, 1);
            MessageBox.Show("return code : " + rc.ToString());
        }
    }
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2

    This might help you.

    It seems as if you've to do:
    Code:
    fixed(long* pPrtHandle = &PrtHandle) {
      ushort rc = PrtOpenDevice(pPrtHandle, 1);
    }
    instead of ushort rc = PrtOpenDevice(& PrtHandle, 1);

    Comment

    • Shirahama Kenichi
      New Member
      • Jun 2010
      • 3

      #3
      I try that one, but it returns "PInvokeStackImb alance was detected" instead the fixed-unfixed error.
      It seems the "pPrtHandle " contains more than one value.
      so the library thinks that "PrtOpenDevice(pPrtHandle, 1)" sending more than two variable.

      Comment

      • Shirahama Kenichi
        New Member
        • Jun 2010
        • 3

        #4
        Originally posted by ChBinder
        http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx
        This might help you.

        It seems as if you've to do:
        Code:
        fixed(long* pPrtHandle = &PrtHandle) {
          ushort rc = PrtOpenDevice(pPrtHandle, 1);
        }
        instead of ushort rc = PrtOpenDevice(& PrtHandle, 1);
        I try something that i haven't yet try:
        i change the code in BOLD:
        Code:
        public static extern ushort PrtOpenDevice([B][System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.U8)][/B] long *PrtHandle, [B]ushort[/B] pPort);


        with:
        Code:
        public static extern ushort PrtOpenDevice(long *PrtHandle, [B]long[/B] pPort);


        thanks to "ChBinder" for the light on "fixed-unfixed error" advice.
        [solved]

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I would have just said maybe this:
          [code=c#]
          public static extern ushort PrtOpenDevice(r ef long PrtHandle, long pPort);
          [/code]
          Which I think would do the same thing?

          Comment

          Working...