Click here to Skip to main content
15,213,699 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have C# windows application which calling C++ unmanaged dll function. this application is working fine at windows XP x86 environment. now i move this application to WIN7 64 bit environment. I complied c++ dll at x64, and build my C# application at x64 as well. when i run my application, I got error message showed "System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." when my application calling c++ dll function.

the code in my C# as show below:

[DllImport("SpotForwardSimDLL.dll")]
private static extern long CalSpotForwardSimulation(int A, int B, int C,
int vDate, int sDate, int eDate, double[] hisCurve, double[] monthVol,
double[] dailyVol, double[] forwardCurves, string[] curveNames,
double[] dailyVal, double[] monthVal,double[] corrVal)

CalSpotForwardSimulation(numFactors, numLoc, numSims,lCobDate, lStartDate, lendDate, hisCurves, monthVol, dailyVol,forwardCurves, curveNames, dailyVal, monthVal, corrVal);



the code in C++ as show below:
__declspec(dllexport) long __stdcall
CalSpotForwardSimulation(int ltempOfFactors, int ltempOfLoc,
int ltempOfSims ,int vDate,int sDate,int eDate,
double * tempCurveHistorical,double * tempMonthlyVol,
double * tempDailyVol,double * tempForwardCurve,
char ** curveName,double dailyRet[],
double monthRet[],double corrRet[])


I have been working on this issue for weeks, any suggestion to solve this problem?
Posted
Updated 3-Mar-11 6:10am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Chances are, you're trying to work with some Win32 executable, like a DLL. You cannot do it, and the problem is only manifested during run-time. For example, how do you know "SpotForwardSimDLL.dll" is not 32-bit?

The components built as 64-bit and 32-bit work on a 64-bit systems in a different platforms. Your 64-bit code runs on the native platform, but a 32-bit process works on the platform called WoW64, see http://en.wikipedia.org/wiki/WOW64[^].

Moreover, there are two separate 64-bit platforms: x86-64 (http://en.wikipedia.org/wiki/X86-64[^]) and IA-64 "Itanium" (http://en.wikipedia.org/wiki/Itanium[^]). They are incompatible. You version of OS supports one of them, depending on your CPU architectures, so all 64-bit software components you obtain should match one of these two architectures, but any 32-bit component will work. If you accidentally try to mix them up, you will get run-time crash of the same kind.

The only option to use a 32-bit component in 64-bit application is running both as different processes and use any kind in inter-process communications between them.

Another simple resolution would be keeping all of your 32-bit codes; chances are, they will successfully run on top of WoW64.

—SA
   
v5
Comments
   
Thank you for accepting my Answer.
Did you solve your problem though?
--SA
IvyLi 3-Mar-11 14:38pm
   
how can i make sure that SpotForwardSimDLL.dll is build at 64-bit? my vs.net is installed at program file(x86). are you saying that even i changed my configuration to x64 the dll generated might not 64-bit?
   
No, no. You see, I don't see you project(s), so I don't know what components you're using. Of course, what you compile will be compiled to the platform you want, but how about other linked components, statically or dynamically? I only know that what worked in win32 stopped to work, and that the only reason I can see.
You know what... Get http://www.dependencywalker.com/, it will help you to study your case.
--SA
   
OP commented:

how can i make sure that SpotForwardSimDLL.dll is build at 64-bit? my vs.net is installed at program file(x86).
   
VS.NET may run as x86, it does not matter. What matters is that the platform your compilation is targeted for.
--SA
IvyLi 3-Mar-11 16:24pm
   
thank you for your information. i will keep trying.
   
You're welcome.
Thank you for accepting my Answer.
Good luck, call again.
--SA
Espen Harlinn 24-Mar-11 3:50am
   
Seems your advice was spot on, my 5 :)
   
Thank you, Espen.
--SA
phantom_ml 3-Nov-14 1:30am
   
Hi, did you solve this problem? Now I have same problem. But I can't find solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100