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. dll used in C# application in VS2005 works correctly, but didn't work in VS2012.

dll used in C# application in VS2005 works correctly, but didn't work in VS2012.

Scheduled Pinned Locked Moved C#
c++csharpvisual-studiodebugginghelp
4 Posts 3 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.
  • S Offline
    S Offline
    sreehari_mysore
    wrote on last edited by
    #1

    I am using a dll developed in Visual c++ in to the C# application. It works fine with Visual Studio 2005. But when using the same with Visual Studio 2012, it causes the error.

    myDll.h

    #ifndef _MYDLL_H
    #define _MYDLL_H

    extern "C" _declspec(dllexport) char* __stdcall getName();

    #endif

    myDll.cpp

    #include "stdafx.h"

    #include "mydll.h"

    extern "C" __declspec(dllexport) char* __stdcall getName()
    {
    return "Hello !!!";
    }

    the above code produces the mydll.dll. I used to copy the dll into newly created c# windows application into bin/Debug directory. The c# application code is somewhat like

    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    namespace sysWinApp
    {
    public partial class Form1 : Form
    {
    [DllImport("myDll.dll")]
    static extern string getName();

        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1\_Click(object sender, EventArgs e)
        {
            label1.Text = getName();
        }
    }
    

    }

    the windows form consists of a label and a button. By clicking the button, it should display the content on the label, which is accessed from the string returned from the dll function. It works fine with VS2005 but the same didn't happen with VS2012. The C# win app of VS2012 prompts the message that it "has stopped working". any ideas to resolve this ?

    F B 2 Replies Last reply
    0
    • S sreehari_mysore

      I am using a dll developed in Visual c++ in to the C# application. It works fine with Visual Studio 2005. But when using the same with Visual Studio 2012, it causes the error.

      myDll.h

      #ifndef _MYDLL_H
      #define _MYDLL_H

      extern "C" _declspec(dllexport) char* __stdcall getName();

      #endif

      myDll.cpp

      #include "stdafx.h"

      #include "mydll.h"

      extern "C" __declspec(dllexport) char* __stdcall getName()
      {
      return "Hello !!!";
      }

      the above code produces the mydll.dll. I used to copy the dll into newly created c# windows application into bin/Debug directory. The c# application code is somewhat like

      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      using System.Runtime.InteropServices;

      namespace sysWinApp
      {
      public partial class Form1 : Form
      {
      [DllImport("myDll.dll")]
      static extern string getName();

          public Form1()
          {
              InitializeComponent();
          }
      
          private void button1\_Click(object sender, EventArgs e)
          {
              label1.Text = getName();
          }
      }
      

      }

      the windows form consists of a label and a button. By clicking the button, it should display the content on the label, which is accessed from the string returned from the dll function. It works fine with VS2005 but the same didn't happen with VS2012. The C# win app of VS2012 prompts the message that it "has stopped working". any ideas to resolve this ?

      F Offline
      F Offline
      Freak30
      wrote on last edited by
      #2

      I'm not sure how you ever got this working in VS2005. There are two issues. First: You can't return a pointer to a char array that you allocated on the stack. The moment the funtion returns the memory is deallocated and a pointer to an unallocated address is returned. You need to allocate the memory using new. This implies that you need to export an additional function that receives a pointer and deletes it (to prevent memory leaks). Second: In your C# application you define (incorrectly) that the imported function returns a (managed) string. Actually it returns a char pointer. I think you will need an unsafe block to receive the char pointer, initialize a new managed string with it and then delete the char pointer (using the function mentioned above). After that you can assign the managed string to the label.

      The good thing about pessimism is, that you are always either right or pleasently surprised.

      S 1 Reply Last reply
      0
      • F Freak30

        I'm not sure how you ever got this working in VS2005. There are two issues. First: You can't return a pointer to a char array that you allocated on the stack. The moment the funtion returns the memory is deallocated and a pointer to an unallocated address is returned. You need to allocate the memory using new. This implies that you need to export an additional function that receives a pointer and deletes it (to prevent memory leaks). Second: In your C# application you define (incorrectly) that the imported function returns a (managed) string. Actually it returns a char pointer. I think you will need an unsafe block to receive the char pointer, initialize a new managed string with it and then delete the char pointer (using the function mentioned above). After that you can assign the managed string to the label.

        The good thing about pessimism is, that you are always either right or pleasently surprised.

        S Offline
        S Offline
        sreehari_mysore
        wrote on last edited by
        #3

        I developed this application by referring to the example in codeproject. please refer the below link Using Unmanaged code and assembler in C#[^]

        1 Reply Last reply
        0
        • S sreehari_mysore

          I am using a dll developed in Visual c++ in to the C# application. It works fine with Visual Studio 2005. But when using the same with Visual Studio 2012, it causes the error.

          myDll.h

          #ifndef _MYDLL_H
          #define _MYDLL_H

          extern "C" _declspec(dllexport) char* __stdcall getName();

          #endif

          myDll.cpp

          #include "stdafx.h"

          #include "mydll.h"

          extern "C" __declspec(dllexport) char* __stdcall getName()
          {
          return "Hello !!!";
          }

          the above code produces the mydll.dll. I used to copy the dll into newly created c# windows application into bin/Debug directory. The c# application code is somewhat like

          using System.Text;
          using System.Threading.Tasks;
          using System.Windows.Forms;
          using System.Runtime.InteropServices;

          namespace sysWinApp
          {
          public partial class Form1 : Form
          {
          [DllImport("myDll.dll")]
          static extern string getName();

              public Form1()
              {
                  InitializeComponent();
              }
          
              private void button1\_Click(object sender, EventArgs e)
              {
                  label1.Text = getName();
              }
          }
          

          }

          the windows form consists of a label and a button. By clicking the button, it should display the content on the label, which is accessed from the string returned from the dll function. It works fine with VS2005 but the same didn't happen with VS2012. The C# win app of VS2012 prompts the message that it "has stopped working". any ideas to resolve this ?

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          The C dll is a 32bit dll (as was your VS2005 application), while your VS2012 application is a 64bit application (or a bitness-ignoring application on a 64bit system).

          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