Click here to Skip to main content
15,887,871 members
Please Sign up or sign in to vote.
5.00/5 (1 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:

C#
[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:
C++
__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

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
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 3-Mar-11 14:30pm    
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?
Sergey Alexandrovich Kryukov 3-Mar-11 15:56pm    
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
Sergey Alexandrovich Kryukov 3-Mar-11 15:59pm    
OP commented:

how can i make sure that SpotForwardSimDLL.dll is build at 64-bit? my vs.net is installed at program file(x86).
Sergey Alexandrovich Kryukov 3-Mar-11 16:00pm    
VS.NET may run as x86, it does not matter. What matters is that the platform your compilation is targeted for.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900