Solved

string to sbyte* conversion error

Posted on 2006-06-28
16
3,231 Views
Last Modified: 2012-06-27
Hi,

      I have an open question in VC++.NET area. I got helped for part of the question. For the rest I need to post here. Below is the link to the question.

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VisualC_PLUS_PLUS_DOT_NET/Q_21901100.html#17004595

      In short, I have an unmanaged dll in MFC C++ with signature

       void DoConversion(const char* videoIn, const char* videoOut)

     Then there is a managed wrapper class in C++.NET with function call like below

 public:
            [DllImport("anothertestproc.dll",
          EntryPoint = "?convertd@CanothertestprocApp@@QAEXPBD0@Z",
              CallingConvention = CallingConvention::ThisCall)]
        static void convertd(const char* videoIn, const char* videoOut);

      Now, I want to use this managed wrapper dll in my C# code with the signature as

            void convertd(string, string).

    But when I add the managed wrapper class as a reference, I get the signature as void convertd(sbyte*, sbyte*). I don't know why it needs that format. How do I manage this?

     
     Please help. Thanks.
0
Comment
Question by:payal1711
16 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17007466
Please describe exactly what function do you want to call from C#: DoConversion or convertd. If you want to call convertd, how does in look in manage C++ project?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17008747
Here is some light reading:

Stan Lippman's BLog -- "The absence of const support in the base class library…"
http://blogs.msdn.com/slippman/archive/2004/01/22/61712.aspx

Why doesn't C# have "const"?
http://blogs.msdn.com/oldnewthing/archive/2004/04/27/121049.aspx

The C++ 'const' Declaration: Why & How
http://duramecho.com/ComputerInformation/WhyHowCppConst.html

Was there a specific reason for the 'const' char*?

Bob
0
 
LVL 1

Author Comment

by:payal1711
ID: 17011277

  Thanks AlexFM. I want to call convertd from my C# project. I have not overriden the method in my managed project. Here is how it looks in the unmanaged code and the managed code:

     - unmanaged code

       void CanothertestprocApp::convertd(const char* videoIn, const char* videoOut)  
{
            DoConversion(videoIn, videoOut);
}

  - managed code

  public:
            [DllImport("anothertestproc.dll",
          EntryPoint = "?convertd@CanothertestprocApp@@QAEXPBD0@Z",
              CallingConvention = CallingConvention::ThisCall)]
                             static void convertd(const char* videoIn, const char* videoOut);

      
     Thanks Bob for the links, I will read them and hopefully find something there. The reason for using const char* is the multimedia library that I am using has this type as its input type in the methods.
   
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17011487
I was wondering if you had control over the library, but it sounds like you don't.  Have you been able to get it to compile with sbyte* parameter?  I am also curious if you can't cast the sbyte* to a string explicitly.

Bob
0
 
LVL 1

Author Comment

by:payal1711
ID: 17011522

   No, I cannot compile with sbyte* parameter and I didn't find any way to successfully convert a string to sbyte*. Looks like sbyte is used for string numericals and not string chars.
0
 
LVL 1

Author Comment

by:payal1711
ID: 17011544

     I forgot to mention one more thing that CString also works. Because that can be casted to const char*. So if you have any solution using CString type that will also work.

     I have tried that but with no luck.

     Thanks.
   
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17011801
I'm sorry, but I didn't understand that last comment.  Are you saying that the C++ arguments would be CString?

Bob
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:payal1711
ID: 17011860

   Yes. Below is what I meant. CString in the signature instead of const char*.
 
  - unmanaged code

       void CanothertestprocApp::convertd(CString videoIn, CString videoOut)  
{
          DoConversion(videoIn, videoOut);
}

  - managed code

  public:
          [DllImport("anothertestproc.dll",
          EntryPoint = "?convertd@CanothertestprocApp@@QAEXPBD0@Z",
            CallingConvention = CallingConvention::ThisCall)]
                             static void convertd(CString videoIn, CString videoOut);

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17011878
Here is a p/invoke response to a question about CString:

http://www.dotnet247.com/247reference/msgs/49/245492.aspx

<Quote>
extern APIENTRY WINAPI void F1(CString str1,CString str2)
{
F2(str1,str2);
}

/*intern*/ void F2(CString str1,CString str2)
{
//do stuffs ..;
}

I have this C# code :

[DllImport("my.dll", EntryPoint = "F1")]
public static extern bool csFct(string s1 ,string s2);
</Quote>

Bob
0
 
LVL 1

Author Comment

by:payal1711
ID: 17011923

  I have already tried this. It doesn't implicitly convert from CStirng to .net String. I came through this post while searching for the same.

   Thanks.
0
 
LVL 1

Author Comment

by:payal1711
ID: 17012009

   This is what I get as error: System.AccessViolationException: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string p2 = "C:\\bla105_050a_008_cp_001.avi";
            string p3 = "C:\\New Folder 2\\newimage_0001.tga";

            convertd(p2, p3);
        }

        [DllImport("anothertestproc.dll",
          EntryPoint = "?convertd@CanothertestprocApp@@QAEXV?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@0@Z",
            CallingConvention = CallingConvention.ThisCall)]
        private static extern void convertd(string videoIn, string videoOut);

    }
0
 
LVL 1

Author Comment

by:payal1711
ID: 17012023

    I think the error is that it cannot access the DLL. Where should I place the dll so that it can see it?
     
0
 
LVL 1

Author Comment

by:payal1711
ID: 17019072

   Thanks Bob for your help. I did receive my answer from the first thread for this question: the link is here:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VisualC_PLUS_PLUS_DOT_NET/Q_21901100.html

   Moderator, can you please refund me the points.

  Thanks everybody.
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 17182946
PAQed with points refunded (100)

DarthMod
Community Support Moderator
0
 

Expert Comment

by:masums
ID: 23643274
User ref class to wrap a unmanaged class
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now