During the project development process, this situation is inevitable: the main program uses the target platform of X86 because the third-party library of the algorithm needs to be X86, but when calling other companies' programs or other program drivers, it cannot be X86 (it is no problem using x64 or Any cup).
I'm encountering an exception connecting to the oracle database "BadImageFormatException is raised when trying to load the Oracle client library. This problem will occur if running in 64-bit mode when the 32-bit Oracle client component is installed". I can't help but complain that the oracle database is really a lot of problems, a lot of problems.
What should I do if this happens? The requirements of the platforms on both sides are different and cannot be modified.
The first method: use other libraries that do not need to distinguish the target platform for connection;
The second method: write an exe program, and its docking operations are implemented in this program and are called by the main program.
Let’s talk about the implementation of the second method.
1. Create a new "Console Application"--Receive the passed parameters in the Main() function in the project startup class file Program; the code is as follows:
public class Program { public static void Main(string[] args) { if( > 0 && !(args[0])) { string num = args[0];//Get the passed parameters //DSCommuncationInfo Custom class; GetOracleData: Connect to Oracle database and get data functions DSCommuncationInfo info = GetOracleData(num); if (info != null) { //Serialize into string array string result = (info); //Write the specified string value (subsequent to the current line terminator) to the standard output stream. (result); } } } }
2. When the main program calls exe, the exe is started using the process method, and the calling code is as follows:
public void StartExternalProgram(string examinerNo) { // Here, the exe is called in the dll program, the path is to get the path where the dll is located string exePath = (().Location); string exeName = ""; string fileName = (exePath, exeName); //User process Process myProcess = new Process(); = false; = true; = fileName; = true; //Pass parameters are separated by spaces. If a parameter is empty, you can pass in "" = examinerNo; = exePath;//Set the initial directory of the process to be started ();//start up (15000);//Waiting for the exe program to complete processing, timeout 15 seconds string xmldata = ();//Read memory stream data in exe if (!(xmldata)) { //Serialization implemented by yourself var info = (xmldata); } }
Although this method is relatively complex, it is also one of the feasible methods to solve platform incompatibility.
() Problem of not starting exe program:
1. It may be that the parameters are not absolute paths, and the path address of the exe is incorrect.
2. If there is no problem running the exe program directly externally, and when there is a configuration file in this program, or when you need to read other files at startup, you need to set the WorkingDirectory property of StartInfo to the application directory.
This is the article about C# calling EXE file to pass parameters and get the return results. For more related contents of C# calling EXE file, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!