4-Participant
July 17, 2023
Solved
C++ and C# p/invoke Not Working Cannot Start Creo And Throws AccessViolationException
- July 17, 2023
- 2 replies
- 7337 views
Hi all,
I'm using Creo Parametric 7.0.2.
I'm working on trying to use C# p/invoke interop method to call a Creo function from the C++ toolkit.
I have a C++ .exe that will include the Creo async functionality.
It exposes these two functions:
extern "C" __declspec(dllexport) int ConnectToCreo()
{
pfcAsyncConnection_ptr connection = pfcAsyncConnection::Start("C:\\Program Files\\PTC\\Creo 7.0.2.0\\Parametric\\bin\\parametric.exe", NULL);
return 0;
}
extern "C" __declspec(dllexport) int NoCreo()
{
return -1;
}
For the C# side, I created a simple command line app:
using System.Runtime.InteropServices;
namespace CreoInteropPoC;
internal class Program
{
[DllImport("CreoAsync.exe", CallingConvention = CallingConvention.Cdecl)]
public static extern int ConnectToCreo();
[DllImport("CreoAsyncDll.exe")]
private static extern int NoCreo();
static void Main(string[] args)
{
Console.WriteLine(NoCreo());
ConnectToCreo();
Console.ReadKey();
}
}
The C# app calls the function NoCreo and works fine but throws an AccessViolationException when calling ConnectToCreo.
I have also attached a screenshot of the call stack.
Is it possible to use p/invoke to call Creo functions? Can you point me to what I'm doing wrong?

