| |

VerySource

 Forgot password?
 Register
Search
View: 1660|Reply: 14

anxious! Wait online. Questions about calling unmanaged .exe programs in C #?

[Copy link]

1

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-21 09:00:01
| Show all posts |Read mode
The .exe program of an application developed in C ++ (currently also being developed in vs) is referenced in the C # environment using DllImport, and there appears "trying to read or write protected memory. This usually indicates that other memory is corrupted. "error. I hope you masters to solve it.
C ++:
EXTERN_C int _declspec (dllexport) GetInt ()
{
Ranch
CFile f ("C:\\t.txt", CFile :: modeReadWrite); // If you add this sentence, an error will occur. If you log out, the program can be executed smoothly and the file path is fine.

return 222;
}

c #:
[DllImport ("DrawSystem1.exe", EntryPoint = "_GetInt @ 0", CharSet = CharSet.Auto)]
        public static extern int GetInt ();

Thank you all masters, this issue must be resolved today. It's been several days, if it can't be resolved, it will delay the process.
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-4-30 12:00:01
| Show all posts
Does the current user have permission to t.txt?
Reply

Use magic Report

0

Threads

52

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-4-30 13:00:01
| Show all posts
Check if the file's attributes are correct
Reply

Use magic Report

1

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-4-30 20:30:01
| Show all posts
The problem seems not to be in the file.
I use the following to test it is not easy to use.
EXTERN_C int _declspec (dllexport) GetInt ()
{
The
// CFile f ("C:\\t.txt", CFile :: modeReadWrite);
MessageBox (0, "TTTTT", "CCCCC", 0);

return 222;
}

Please also help to analyze
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-5-1 20:15:02
| Show all posts
Why use the method in EXE? Is it bad to use DLL?
Reply

Use magic Report

1

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-5-2 19:30:01
| Show all posts
Yes, but the effect is the same!
I was also new to this thing, depressed. Also hope that you masters help!
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-5-6 09:45:01
| Show all posts
Try to change the c ++ function calling method to stdcall (guess)
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-5-7 09:30:02
| Show all posts
I have the same problem, my C ++ dll header file is as follows:
namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        // Returns a + b
        static __declspec (dllexport) double Add (double a, double b);
        
// Returns a-b
        static __declspec (dllexport) double Subtract (double a, double b);

        // Returns a * b
        static __declspec (dllexport) double Multiply (double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static __declspec (dllexport) double Divide (double a, double b);
    };
}
Then it can be called in C ++, but encountered problems in C #.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MyCSharpExecRefsDll
{
    class Program
    {
        static void Main (string [] args)
        {
            myWin32 mywin32 = new myWin32 ();
            Console.WriteLine (myWin32.Add (3, 4));

        }
            public class myWin32
        {
            [DllImport ("MathFuncsDll1.dll")]
            public static extern double Add (double a, double b);
            [DllImport ("MathFuncsDll1.dll")]
            public static extern double Subtract (double a, double b);
            [DllImport ("MathFuncsDll1.dll")]
            public static extern double Multiply (double a, double b);
            [DllImport ("MathFuncsDll1.dll")]
            public static extern double Divide (double a, double b);

        }

    }
}
When calling, always say there is no such entry.
Unable to find entry point named "Add" in DLL "MathFuncDll1.dll"
Reply

Use magic Report

1

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-5-7 22:00:01
| Show all posts
If you write it like this, you can't find the entry point of the program.
EXTERN_C void __stdcall GetInt ()
{
The
// CFile f ("C:\\t.txt", CFile :: modeReadWrite);
MessageBox (0, "TTTTT", "CCCCC", 0);

// return 222;
}
Reply

Use magic Report

1

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-5-8 08:45:01
| Show all posts
TO:freegreat
You write the method outside the class, it should be fine. Then you use depends to open the dll file you edited, in which you can view the entry function name.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list