Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Object reference not set to an instance of an object.

Object reference not set to an instance of an object.

Scheduled Pinned Locked Moved C#
csharpc++question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HJo
    wrote on last edited by
    #1

    Hi, I am trying to create a C# interface for a dll with functions. The C++ interface for one of the functions looks like this: __declspec(dllimport) long _cdecl GetLiveList( long, long*, long*, long[]*, long); I declared my C# equivalent as: public class Ismbus32 { //... [DllImport("ismbus32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)] private static extern long GetLiveList( long, ref long, ref long, ref long[], long); } When I try to call the function, using the code below, I get a NullPointerException and the message "Object reference not set to an instance of an object." public class Form1 : System.Windows.Forms.Form { //... private void btn_Click(object sender, System.EventArgs e) { long in = 2; long out1 = 0; long out2 = 0; long[] items = new long[635]; long itemsMax = 634; long lRes = Ismbus32._CDDLG_GetLiveList(in, ref out1, ref out2, ref items, itemsMax); } } Does anyone have a clue?! Thanks in advance, /Henrik Johansson

    R 1 Reply Last reply
    0
    • H HJo

      Hi, I am trying to create a C# interface for a dll with functions. The C++ interface for one of the functions looks like this: __declspec(dllimport) long _cdecl GetLiveList( long, long*, long*, long[]*, long); I declared my C# equivalent as: public class Ismbus32 { //... [DllImport("ismbus32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)] private static extern long GetLiveList( long, ref long, ref long, ref long[], long); } When I try to call the function, using the code below, I get a NullPointerException and the message "Object reference not set to an instance of an object." public class Form1 : System.Windows.Forms.Form { //... private void btn_Click(object sender, System.EventArgs e) { long in = 2; long out1 = 0; long out2 = 0; long[] items = new long[635]; long itemsMax = 634; long lRes = Ismbus32._CDDLG_GetLiveList(in, ref out1, ref out2, ref items, itemsMax); } } Does anyone have a clue?! Thanks in advance, /Henrik Johansson

      R Offline
      R Offline
      Roger Stewart
      wrote on last edited by
      #2

      It looks as if you have a class, Ismbus32, that wrappers the ismbus32.dll. If so did you create an instance of this class?? For example:

      private void Form1_Load(object sender, System.EventArgs e)
      {
          Ismbus32= new Ismbus32();
      }
      

      Also, you have GetLiveList() defined in your C++ dll as dllimport. Shouldn't you have it defined as dllexport?? Roger Stewart "I Owe, I Owe, it's off to work I go..."

      H 1 Reply Last reply
      0
      • R Roger Stewart

        It looks as if you have a class, Ismbus32, that wrappers the ismbus32.dll. If so did you create an instance of this class?? For example:

        private void Form1_Load(object sender, System.EventArgs e)
        {
            Ismbus32= new Ismbus32();
        }
        

        Also, you have GetLiveList() defined in your C++ dll as dllimport. Shouldn't you have it defined as dllexport?? Roger Stewart "I Owe, I Owe, it's off to work I go..."

        H Offline
        H Offline
        HJo
        wrote on last edited by
        #3

        Thank you for your reply! There is no need to create an instance, since the methods are declared static. I'm sure GetLiveList() is declared dllexport in the dll. The C++ code was a header file used when importing the dll into C++ code, which I have used as a model for my C# interface. The problem turned out to be the difference of the long data type between C++ and C#: C++ long is 4 bytes whereas C# long is 8 bytes and C# int is 4 bytes. So the solution was to replace all occurences of long in the interface with int. :) Regards, /Henrik

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups