473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C# calling C++ dll (Instruction at referenced memory could not be read)

Hi,

I have a C# assembly that is calling directly into C++ unmanaged
code. Everything is working fine but at the very end of the process
I
get an application error, which says:

"The instruction at (hexNumber) referenced memory at (hexNumber). The
memory could not be read. Click on OK to terminate the program."

It seems to me that the .Net CLR is trying to unload the C++ dll but
hits an issue when it tries to free up some resources. I have read
that this can sometimes occur if there is a buffer overflow. For
example, if you pass in a string or array (by ref) and the C++ code
overruns the array. However, I am not sure if this is my problem.

I am passing in a number of "ref" params into the C++ function, these
are all arrays. I am passing in strings but these are "by value". I
have checked all the values going in and coming out and they all look
the exact same.

Any ideas on his would be greatly appreciated..

Thanks!
gwell

Aug 8 '07 #1
6 2291
gwell wrote:
Hi,

I have a C# assembly that is calling directly into C++ unmanaged
code. Everything is working fine but at the very end of the process
I
get an application error, which says:

"The instruction at (hexNumber) referenced memory at (hexNumber). The
memory could not be read. Click on OK to terminate the program."

It seems to me that the .Net CLR is trying to unload the C++ dll but
hits an issue when it tries to free up some resources. I have read
that this can sometimes occur if there is a buffer overflow. For
example, if you pass in a string or array (by ref) and the C++ code
overruns the array. However, I am not sure if this is my problem.

I am passing in a number of "ref" params into the C++ function, these
are all arrays. I am passing in strings but these are "by value". I
have checked all the values going in and coming out and they all look
the exact same.

Any ideas on his would be greatly appreciated..

Thanks!
gwell
When you call unmanaged code, you have to pin the objects that you send
to it. The garbage collector can move managed object at any time, so you
have to tell it not to move the objects that the unmanaged code is using.

Use the fixed keyword to pin objects in memory.

--
Göran Andersson
_____
http://www.guffa.com
Aug 8 '07 #2
>Any ideas on his would be greatly appreciated..

Please post relevant parts of your code, it's hard to guess what's
wrong without it.

>When you call unmanaged code, you have to pin the objects that you send
to it.
Sometimes you do, but usually you don't have to. The CLR takes care of
pinning arguments for the duration of the call if needed.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 8 '07 #3
On Aug 8, 9:57 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
Any ideas on his would be greatly appreciated..

Please post relevant parts of your code, it's hard to guess what's
wrong without it.
When you call unmanaged code, you have to pin the objects that you send
to it.

Sometimes you do, but usually you don't have to. The CLR takes care of
pinning arguments for the duration of the call if needed.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
Hi,

Here is my code.. I would be grateful if you could spot anything...
thanks

Note, some of these values (e.g. billRates is actually an array of
Double values). In my code I pass in the first element of the array
and I believe the C++ code can then use the pointer to get the rest of
the array. Is this OK?

Also, if I add a "ref" keyword to the two StringBuilder parameters the
DLL throws an exception. This seems strange seeing that the parameter
is declared as a pointer....

// C++ DLL Function
int WINAPI constructYC(long referenceDate,
char * currency,
double * billRates,
long * billEndDates,
bool * useBills,
double * billBumpAmounts,
long billSpotLag,
long billMaturityDatesLength,
long billsConvention,
double * billFuturesRates,
long * billFuturesStartDates,
long * billFuturesEndDates,
bool * useBillFutures,
double * billFuturesBumpAmounts,
long billFuturesStartDatesLength,
long billFuturesConvention,
double * specialDatesRates,
long * specialDatesStartDates,
long * specialDatesEndDates,
bool * useSpecialDates,
double * specialDatesBumpAmounts,
long specialStartDatesLength,
long specialDatesConvention,
double * bondCoupons,
double * swapRatesOrBondYields,
double * swapTenorsOrMaturities,
long * bondMaturityDates,
long * swapOrBondFrequencies,
bool * useSwapsBonds,
double * swapBondBumpAmounts,
long swapBondSpotLag,
long swapTenorsOrBondMaturityDatesLength,
long swapsOrBondsConvention,
bool enforceLinInterp,
long buildMethod,
bool billsOverrideFRAsAtStart,
bool billsOverrideFRAsAfterStart,
char * ycName,
long *holidayDates)
// C# P/Invoke method
[System.Runtime.InteropServices.DllImport(@"TheDLL. dll", EntryPoint =
"constructYCDll", CharSet = CharSet.Ansi)]
private static extern Int32 constructYCDll(
Int32 referenceDate,
StringBuilder currency,
ref Double billRates,
ref Int32 billEndDates,
ref Boolean useBills,
ref Double BillBumps,
Int32 billSpotLag,
Int32 billMaturityDatesLength,
Int32 billsConvention,
ref Double billFuturesRates,
ref Int32 billFuturesStartDates,
ref Int32 billFuturesEndDates,
ref Boolean useBillFutures,
ref Double BillFuturesBumps,
Int32 billFuturesStartDatesLength,
Int32 billFuturesConvention,
ref Double specialDatesRates,
ref Int32 specialDatesStartDates,
ref Int32 specialDatesEndDates,
ref Boolean useSpecialDates,
ref Double SpecialDatesBumps,
Int32 specialStartDatesLength,
Int32 specialDatesConvention,
ref Double bondCoupons,
ref Double swapRates,
ref Double swapTenors,
ref Int32 bondMaturityDates,
ref Int32 swapFrequencies,
ref Boolean useSwaps,
ref Double SwapBumps,
Int32 swapSpotLag,
Int32 swapTenorsLength,
Int32 swapsOrBondsConvention,
Boolean enforceLinInterp,
Int32 buildMethod,
Boolean billsOverrideFRAsAtStart,
Boolean billsOverrideFRAsAfterStart,
StringBuilder ycName,
ref Int32 holidayDates
);
// C# code to call the array
i = constructYCDll(refdate,
ccyCstring,
ref billRates[0],
ref billEndDates[0],
ref useBills[0],
ref billBumps[0],
billSpotLag,

billMaturityDatesLength,
shortConvention,
ref
billFuturesRates[0],
ref
billFuturesStartDates[0],
ref
billFuturesEndDates[0],
ref
useBillFutures[0],
ref
billFuturesBumps[0],

billFuturesStartDatesLength,
shortSpotLag,
ref
specialDatesRates[0],
ref
specialDatesStartDates[0],
ref
specialDatesEndDates[0],
ref
useSpecialDates[0],
ref
specialDatesBumps[0],

specialStartDatesLength,
shortSpotLag,
ref bondCoupons[0],
ref swapRates[0],
ref swapTenors[0],
ref
bondMaturityDates[0],
ref
swapFrequencies[0],
ref useSwaps[0],
ref swapBumps[0],
swapSpotLag,
swapTenorsLength,
longConvention,
enforceLinInterp,
buildMethod,

billsOverrideFRAsAtStart,

billsOverrideFRAsAfterStart,
ycName,
ref holidays[0]);
Aug 9 '07 #4
>Note, some of these values (e.g. billRates is actually an array of
Double values). In my code I pass in the first element of the array
and I believe the C++ code can then use the pointer to get the rest of
the array. Is this OK?
Nope, you should pass the entire array. Change the parameter type to
double[].
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 9 '07 #5
On Aug 9, 3:25 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
Note, some of these values (e.g. billRates is actually an array of
Double values). In my code I pass in the first element of the array
and I believe the C++ code can then use the pointer to get the rest of
the array. Is this OK?

Nope, you should pass the entire array. Change the parameter type to
double[].

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
Hi I tried your suggestion and things work but I still get the
error... here is my code now if you can spot anything else? I am
going to create stubs this morning in C++ and try to narrow down
things... i think its going to be a slow process... thanks for your
help!

// C# P/Invoke method
[System.Runtime.InteropServices.DllImport(@"TheDLL. dll", EntryPoint =
"constructYCDll", CharSet = CharSet.Ansi)]
private static extern Int32 constructYC(
Int32 referenceDate,
StringBuilder currency, // This needs ref keyword
[In, Out] Double[] billRates,
[In, Out] Int32[] billEndDates,
[In, Out] Boolean[] useBills,
[In, Out] Double[] BillBumps,
Int32 billSpotLag,
Int32 billMaturityDatesLength,
Int32 billsConvention,
[In, Out] Double[] billFuturesRates,
[In, Out] Int32[] billFuturesStartDates,
[In, Out] Int32[] billFuturesEndDates,
[In, Out] Boolean[] useBillFutures,
[In, Out] Double[] BillFuturesBumps,
Int32 billFuturesStartDatesLength,
Int32 billFuturesConvention,
[In, Out] Double[] specialDatesRates,
[In, Out] Int32[] specialDatesStartDates,
[In, Out] Int32[] specialDatesEndDates,
[In, Out] Boolean[] useSpecialDates,
[In, Out] Double[] SpecialDatesBumps,
Int32 specialStartDatesLength,
Int32 specialDatesConvention,
[In, Out] Double[] bondCoupons,
[In, Out] Double[] swapRates,
[In, Out] Double[] swapTenors,
[In, Out] Int32[] bondMaturityDates,
[In, Out] Int32[] swapFrequencies,
[In, Out] Boolean[] useSwaps,
[In, Out] Double[] SwapBumps,
Int32 swapSpotLag,
Int32 swapTenorsLength,
Int32 swapsOrBondsConvention,
Boolean enforceLinInterp,
Int32 buildMethod,
Boolean billsOverrideFRAsAtStart,
Boolean billsOverrideFRAsAfterStart,
StringBuilder ycName, // This needs ref keyword
[In, Out] Int32[] holidayDates
);

// Call to method
i = constructYC(refdate,
ccyCstring,
billRates,
billEndDates,
useBills,
billBumps,
billSpotLag,

billMaturityDatesLength,
shortConvention,
billFuturesRates,

billFuturesStartDates,
billFuturesEndDates,
useBillFutures,
billFuturesBumps,

billFuturesStartDatesLength,
shortSpotLag,
specialDatesRates,

specialDatesStartDates,
specialDatesEndDates,
useSpecialDates,
specialDatesBumps,

specialStartDatesLength,
shortSpotLag,
bondCoupons,
swapRates,
swapTenors,
bondMaturityDates,
swapFrequencies,
useSwaps,
swapBumps,
swapSpotLag,
swapTenorsLength,
longConvention,
enforceLinInterp,
buildMethod,

billsOverrideFRAsAtStart,

billsOverrideFRAsAfterStart,
ycName,
holidays);

Aug 9 '07 #6
On Aug 10, 8:29 am, gwell <gavincolw...@gmail.comwrote:
On Aug 9, 3:25 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
>Note, some of these values (e.g. billRates is actually an array of
>Double values). In my code I pass in the first element of the array
>and I believe the C++ code can then use the pointer to get the rest of
>the array. Is this OK?
Nope, you should pass the entire array. Change the parameter type to
double[].
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.

Hi I tried your suggestion and things work but I still get the
error... here is my code now if you can spot anything else? I am
going to create stubs this morning in C++ and try to narrow down
things... i think its going to be a slow process... thanks for your
help!

// C# P/Invoke method
[System.Runtime.InteropServices.DllImport(@"TheDLL. dll", EntryPoint =
"constructYCDll", CharSet = CharSet.Ansi)]
private static extern Int32 constructYC(
Int32 referenceDate,
StringBuilder currency, // This needs ref keyword
[In, Out] Double[] billRates,
[In, Out] Int32[] billEndDates,
[In, Out] Boolean[] useBills,
[In, Out] Double[] BillBumps,
Int32 billSpotLag,
Int32 billMaturityDatesLength,
Int32 billsConvention,
[In, Out] Double[] billFuturesRates,
[In, Out] Int32[] billFuturesStartDates,
[In, Out] Int32[] billFuturesEndDates,
[In, Out] Boolean[] useBillFutures,
[In, Out] Double[] BillFuturesBumps,
Int32 billFuturesStartDatesLength,
Int32 billFuturesConvention,
[In, Out] Double[] specialDatesRates,
[In, Out] Int32[] specialDatesStartDates,
[In, Out] Int32[] specialDatesEndDates,
[In, Out] Boolean[] useSpecialDates,
[In, Out] Double[] SpecialDatesBumps,
Int32 specialStartDatesLength,
Int32 specialDatesConvention,
[In, Out] Double[] bondCoupons,
[In, Out] Double[] swapRates,
[In, Out] Double[] swapTenors,
[In, Out] Int32[] bondMaturityDates,
[In, Out] Int32[] swapFrequencies,
[In, Out] Boolean[] useSwaps,
[In, Out] Double[] SwapBumps,
Int32 swapSpotLag,
Int32 swapTenorsLength,
Int32 swapsOrBondsConvention,
Boolean enforceLinInterp,
Int32 buildMethod,
Boolean billsOverrideFRAsAtStart,
Boolean billsOverrideFRAsAfterStart,
StringBuilder ycName, // This needs ref keyword
[In, Out] Int32[] holidayDates
);

// Call to method
i = constructYC(refdate,
ccyCstring,
billRates,
billEndDates,
useBills,
billBumps,
billSpotLag,

billMaturityDatesLength,
shortConvention,
billFuturesRates,

billFuturesStartDates,
billFuturesEndDates,
useBillFutures,
billFuturesBumps,

billFuturesStartDatesLength,
shortSpotLag,
specialDatesRates,

specialDatesStartDates,
specialDatesEndDates,
useSpecialDates,
specialDatesBumps,

specialStartDatesLength,
shortSpotLag,
bondCoupons,
swapRates,
swapTenors,
bondMaturityDates,
swapFrequencies,
useSwaps,
swapBumps,
swapSpotLag,
swapTenorsLength,
longConvention,
enforceLinInterp,
buildMethod,

billsOverrideFRAsAtStart,

billsOverrideFRAsAfterStart,
ycName,
holidays);

Hi,

So I tred to create some empty stubs and everything worked fine. After
that I worked through the C++ code and found the issue. There was a
function called "DllMain" (called when unloading the DLL I believe)
that was not freeing up some global variables. Some nice sloppy code
that just needed to be cleaned up.... once this was done no more
errors were raised.... phew.... took a lot of time to narrow down the
issue but the fix was quite simple....

Thanks for all your help.... there were some very valuable lessons
learnt there... Cheers! Gavin
Aug 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Askari | last post by:
Yesterday, ALL code in python work and nothing when I close(finish) a code. Today, when I close, some raise this windows error : Instruction at "0x00FC3D70" use memory address "0x00000000". Can't...
3
by: Justin To via AccessMonster.com | last post by:
I just distributed a FE mde file throught my department, and while stress testing on the performance of the new release, 3 users got the following error: The instruction at ... referenced memory...
0
by: antsays | last post by:
I am trying to serialize a collection to disk using the code provided. This works just fine but when I try do copy and past the xml file to another location or sometimes even just click on the...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
2
by: Jean Stax | last post by:
Hi ! I am looking for a way to call IL instruction from withing C# code. I am not talking about System.Reflection.Emit sort of topics, but rather about existing C# code, where I want to call IL...
0
by: sklett | last post by:
I've create a simple MFC dll to wrap some more complex C++ code that was supplied to me. I want to call the methods in my MFC wrapper dll from my C# app. I have a very simple case where this...
2
by: gwell | last post by:
Hi, I have a C# assembly that is calling directly into C++ unmanaged code. Everything is working fine but at the very end of the process I get an application error, which says: "The...
1
by: Nilam2477 | last post by:
While running my application i got following error in Application log Faulting application .exe, version , faulting module mfc42u.dll, version 6.2.8071.0, fault address 0x0000239d. Corresponding...
2
by: unauthorized | last post by:
The short story: I need to be able to cast a function pointer for any function and class to an intermediate type, so I could call it from any point of my program using the "__asm call" instruction....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.