Getting a CString

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen Engle

    Getting a CString

    I am attempting to write a C# program that accesses a C++ library. The
    library has functions that return CStrings. So far I have not been able to
    find a way to declare the function prototype in C# such that it will work.

    For instance, I have tried declaring the function as a string type but when
    I call the function, it errors out with the following:

    An unhandled exception of type 'System.NullRef erenceException ' occurred in
    TestDLM.exe

    Additional information: Object reference not set to an instance of an
    object.

    I have tried a couple other declarations with no better results.



    Does anyone have any advice?

    Thanks.


  • James Curran

    #2
    Re: Getting a CString

    I don't think there's anyway to get C# to recognize a CString. I'd ssay
    your best bet is to write a wrapper for the C++ library in Managed C++,
    which will convert the CStrings to System.Strings

    --
    --
    Truth,
    James Curran
    [erstwhile VC++ MVP]

    Home: www.noveltheory.com Work: www.njtheater.com
    Blog: www.honestillusion.com Day Job: www.partsearch.com

    "Stephen Engle" <engle.stephen@ aoins.com> wrote in message
    news:euK0YocSFH A.2520@TK2MSFTN GP09.phx.gbl...[color=blue]
    > I am attempting to write a C# program that accesses a C++ library. The
    > library has functions that return CStrings. So far I have not been able[/color]
    to[color=blue]
    > find a way to declare the function prototype in C# such that it will work.
    >
    > For instance, I have tried declaring the function as a string type but[/color]
    when[color=blue]
    > I call the function, it errors out with the following:
    >
    > An unhandled exception of type 'System.NullRef erenceException ' occurred in
    > TestDLM.exe
    >
    > Additional information: Object reference not set to an instance of an
    > object.
    >
    > I have tried a couple other declarations with no better results.
    >
    >
    >
    > Does anyone have any advice?
    >
    > Thanks.
    >
    >[/color]


    Comment

    • Bruce Wood

      #3
      Re: Getting a CString

      Here is an example from my GDI32.cs:


      [DllImport("wins pool.drv", CharSet=CharSet .Auto)]
      static extern bool OpenPrinter(
      [MarshalAs(Unman agedType.LPTStr )] string printerName,
      out IntPtr printerHandle, IntPtr printerDefaults );

      Comment

      • Bruce Wood

        #4
        Re: Getting a CString

        Oh, crap. I just saw that you said your library _returns_ CStrings, not
        accepts them. Sorry for the brain-dead post. :-(

        Comment

        • Stephen Engle

          #5
          Re: Getting a CString

          Thanks. I went with what you posted and that worked well.


          "James Curran" <jamescurran@mv ps.org> wrote in message
          news:uXhqQxcSFH A.2520@TK2MSFTN GP09.phx.gbl...[color=blue]
          > I don't think there's anyway to get C# to recognize a CString. I'd[/color]
          ssay[color=blue]
          > your best bet is to write a wrapper for the C++ library in Managed C++,
          > which will convert the CStrings to System.Strings
          >
          > --
          > --
          > Truth,
          > James Curran
          > [erstwhile VC++ MVP]
          >
          > Home: www.noveltheory.com Work: www.njtheater.com
          > Blog: www.honestillusion.com Day Job: www.partsearch.com
          >
          > "Stephen Engle" <engle.stephen@ aoins.com> wrote in message
          > news:euK0YocSFH A.2520@TK2MSFTN GP09.phx.gbl...[color=green]
          > > I am attempting to write a C# program that accesses a C++ library. The
          > > library has functions that return CStrings. So far I have not been able[/color]
          > to[color=green]
          > > find a way to declare the function prototype in C# such that it will[/color][/color]
          work.[color=blue][color=green]
          > >
          > > For instance, I have tried declaring the function as a string type but[/color]
          > when[color=green]
          > > I call the function, it errors out with the following:
          > >
          > > An unhandled exception of type 'System.NullRef erenceException ' occurred[/color][/color]
          in[color=blue][color=green]
          > > TestDLM.exe
          > >
          > > Additional information: Object reference not set to an instance of an
          > > object.
          > >
          > > I have tried a couple other declarations with no better results.
          > >
          > >
          > >
          > > Does anyone have any advice?
          > >
          > > Thanks.
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...