locked
C# call C++ RRS feed

  • General discussion

  • When C# call C++ more than 60 seconds it will raise an exception.

    C++(.h)

    extern "C"
    {
    	__declspec(dllexport) void PassString2(char *str);
    };

    C++(.cpp)

    void PassString2(char *str)
    {
    	if (0 == strcmp(str, "C#"))
    	{
    		strcpy_s(str, 32, "I'm(我是)C++");
    	}
    }

    C#

    [DllImport("MFCLib.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "PassString2")]
    extern static void PassString2(StringBuilder arg);

    run:

    private void callBtnPassString2_Click(object sender, RoutedEventArgs e)
    {
        StringBuilder sb = new StringBuilder("C#", 256);
        for (int i = 0; i < 10000000; i++)
        {
            PassString2(sb);
            Console.WriteLine("Person out args: {0}", sb);
        }
    }


    • Changed type oneonce Tuesday, August 29, 2017 8:06 AM
    Tuesday, August 29, 2017 3:40 AM

All replies

  • Hi oneonce,

    You have created a thread: The application can not run on windows7/8
    Have you abandoned your previous post?

    Regards,

    Ashidacchi

    Tuesday, August 29, 2017 6:11 AM
  • If previous way can't be solved, i will use this way to call C++. ^_^

    • Edited by oneonce Tuesday, August 29, 2017 6:19 AM
    Tuesday, August 29, 2017 6:17 AM
  • OK.
    It may be an alternative, if new issue will never rise.

    Ashidacchi

    Tuesday, August 29, 2017 6:21 AM
  • Hi,

    Please note that do not post duplicate thread on one question, duplicate posting is not allowed.

    Please consider which forum you're going to post in before posting.

    This is the wrong forum for your question because it's got nothing to do with WPF.

    I see what you are trying to ask, but the policy forbids long conversation in comments.

    I suggest you try to narrow down the cause of issue,  is it a  C# call C++ question ? or is it a deployment issue.

    and what exception it raised?

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    • Edited by Bob Ding Tuesday, August 29, 2017 7:52 AM
    Tuesday, August 29, 2017 7:45 AM
  • :(

    This is another way(not use C++/CLR) for c# to call C++ question! not deployment issue.

    Tuesday, August 29, 2017 8:12 AM
  • Hi,

    This forum is asking and discussing question about WPF. It might be worth asking question in the Visual C#  fourm.

    Note that if your application raise an exception, you should share the exception to us.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 29, 2017 8:22 AM
  • Why this threads can not close this.
    Wednesday, September 13, 2017 7:35 AM
  • Hi,

    This thread type is General discussion, you do not have to close this post. Thanks.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 13, 2017 7:39 AM
  • Hi,

    This thread type is General discussion, you do not have to close this post. Thanks.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    But i can't submit a new thread, tell me: Your number of posts today has exceeded the limit.

    I have not submit new thread today!
    • Edited by oneonce Wednesday, September 13, 2017 7:43 AM
    Wednesday, September 13, 2017 7:42 AM
  • Hi,

    Before that I never met this situation, Please go to the following forum to ask. Thanks.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=reportabug

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 13, 2017 7:50 AM
  • I can not submit thread there too!!!
    Wednesday, September 13, 2017 7:54 AM
  • 1. There is a test.txt, encoding of it is ANSI.

    2. the content of test.txt is:

    a="K黨lwassertemperatur"

    3. the actual content of test.txt is :

    a="Kühlwassertemperatur"


    I read the content to a list(bytes), then convert bytes to UTF-8, but the target string(lineText) is not "Kühlwassertemperatur"

    using (BinaryReader bReader = new BinaryReader(fstream, Encoding.ASCII))
    {
        string lineText = "";
        List<byte> bytes = new List<byte>();
        byte b;
        do
        {
            b = bReader.ReadByte();
            bytes.Add(b);
        } while ('\n' != b);
    
        byte[] u8b = Encoding.Convert(Encoding.ASCII, Encoding.UTF8, bytes.ToArray(), 0, bytes.Count);
        lineText = Encoding.Unicode.GetString(u8b, 0, u8b.Length);
    }


    • Edited by oneonce Wednesday, September 13, 2017 8:08 AM
    Wednesday, September 13, 2017 8:04 AM
  • Hi,

    Maybe you have to try asking questions tomorrow. Thank you for your understanding.

    Check below thread with same question.

    Is there a limit for the number of questions somebody are allowed to post daily

    can't ask a question - Your number of posts today has exceeded the limit - why???

    Please check your Browser, try to open a new thread on another browser (I recommend IE) or another machine .

    Note : Avoid creating duplicate posts.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 13, 2017 8:10 AM