troubleshooting Question

Calling C++ DLL from C#

Avatar of deming
deming asked on
.NET ProgrammingC#Visual C++.NET
7 Comments1 Solution1422 ViewsLast Modified:
I have a WORKING unmanaged DLL written in C++ that I am trying to call from C#.  Here are the declarations from C++:
__declspec(dllimport) BOOL __stdcall SaveStrData(const char *object, int index, const char *value);
__declspec(dllexport) BOOL __stdcall LoadStrData(const char *object, int index, char *retval);
__declspec(dllimport) char *__stdcall LoadStrData2(const char *object, int index);

Here is what I tried in C# in my Class1:
        [DllImport("IECGlobalVars32.dll", CharSet = CharSet.Ansi)]
        public static extern bool SaveStrData(string object1, int index, string sValue);

        [DllImport("IECGlobalVars32.dll", CharSet = CharSet.Ansi)]
        public static extern bool LoadStrData(string object1, int index, string sValue);

        [DllImport("IECGlobalVars32.dll", CharSet = CharSet.Ansi)]
        public static extern string LoadStrData2(string object1, int index);

The LoadStrData and LoadStrData2 both fail.

Here are my calls in C#:
        private void btnSaveStr_Click(object sender, EventArgs e)
        {
            String myStr;
            Int32 myIndex = 0;
            String myObj;
            myObj = tbObj.Text;
            myStr = tbValue.Text;
            myIndex = System.Convert.ToInt32(tbIndex.Text);
            Class1.SaveStrData(myObj, myIndex, myStr);
        }

        private void btnLoadStrData_Click(object sender, EventArgs e)
        {
            string myStr;
            Int32 myIndex = 0;
            String myObj;
            myObj = tbObj.Text;
            myStr = "nothing here";
            myIndex = System.Convert.ToInt32(tbIndex.Text);
            Class1.LoadStrData(myObj, myIndex, myStr);
            tbValue.Text = myStr;

        }

        private void btnLoadStrData2_Click(object sender, EventArgs e)
        {
            String myStr;
            Int32 myIndex = 0;
            String myObj;
            myObj = tbObj.Text;
            myStr = "nothing here";
            myIndex = System.Convert.ToInt32(tbIndex.Text);
            myStr = Class1.LoadStrData2(myObj, myIndex);
            tbValue.Text = myStr;

        }

QUESTION: What changes are necessary to make this work from C#?
ASKER CERTIFIED SOLUTION
AndyAinscow
Freelance programmer / Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros