473,407 Members | 2,598 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,407 software developers and data experts.

how to pass parameter "_bstr_t cons &" to invoke a dll written in C++?

Hi,
I am a complete newbie and these are the first lines of code that I write, so please be patient :(
I need to create a C# exe file that calls a c++ function

What I wrote always throws an error at runtime:

Expand|Select|Wrap|Line Numbers
  1. Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  2.    at DDecrypt.DefaultDecrypt(String s)
  3.    at DDecrypt.Main()
The function it calls is in a C++ dll ()closed source code); exploring the exposed functions I found:
[indent]...
Expand|Select|Wrap|Line Numbers
  1. void CLSCrypt::`default constructor closure'(void)
  2. CLSCrypt::CLSCrypt(bool,class _bstr_t const &)
  3. class _bstr_t CLSCrypt::DecryptString(class _bstr_t const &,enum STRCODING,bool)
  4. class _bstr_t CLSCrypt::DefaultDecrypt(class _bstr_t const &)
  5. class _bstr_t CLSCrypt::DefaultEncrypt(class _bstr_t const &)
  6. void CLSCrypt::Delete(void)
  7. class _bstr_t CLSCrypt::EncryptString(class _bstr_t const &,enum STRCODING)
  8. class _bstr_t CLSCrypt::GetBase64(unsigned char *,unsigned long)
  9. bool CLSCrypt::GetFromBase64(class _bstr_t const &,unsigned char * &,unsigned long &)
  10. bool CLSCrypt::GetFromHEX(class _bstr_t const &,unsigned char * &,unsigned long &)
  11. class _bstr_t CLSCrypt::GetHEX(unsigned char *,unsigned long)
  12. class _bstr_t CLSCrypt::GetMD5String(unsigned char *,unsigned long,enum STRCODING)
  13. void CLSCrypt::SetAccess(class _bstr_t const &,unsigned long)
  14. CLSCrypt::~CLSCrypt(void)

the function I want to call is
Expand|Select|Wrap|Line Numbers
  1. class _bstr_t CLSCrypt::DefaultDecrypt(class _bstr_t const &)
]

and it's mangled code is
Expand|Select|Wrap|Line Numbers
  1. ?DefaultDecrypt@CLSCrypt@@SA?AV_bstr_t@@ABV2@@Z
to test the import of the function I wrote:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. public class DDecrypt
  5. {
  6.     [DllImport("LSUtils.dll", EntryPoint = "?DefaultDecrypt@CLSCrypt@@SA?AV_bstr_t@@ABV2@@Z", ExactSpelling = true)]
  7.     public static extern String DefaultDecrypt(String s);
  8.  
  9.     public static void Main()
  10.     {            
  11.         String s = "this is a test!";
  12.         String y = DefaultDecrypt(s);
  13.         Console.WriteLine(y);
  14.     }
  15. }
when I run it the error is the one above;

I suppose it is due to the data type that I want to pass (a String while the function wants something else)

I tried to import the function as
Expand|Select|Wrap|Line Numbers
  1. public static extern String DefaultDecrypt([MarshalAs(UnmanagedType.BStr)] String s);
or even to mark the code as 'unsafe' and use as input
Expand|Select|Wrap|Line Numbers
  1. IntPtr ptr = Marshal.PtrToStringBSTR(s);
but the error is always the same;

Could somebody help me with this?
Thanks
Mar 1 '10 #1
2 2400
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Mar 1 '10 #2
(thanks for the hint and the corrections to the format as well)
Mar 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: usr2003 | last post by:
I wrote the following test program to test the linkage directives extern "C": #include <stdio.h> extern "C" {
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
1
by: andrew | last post by:
Hi, I'm a C++ newbie, so apologies for this rather basic question. I've searched for help and, while I understand the problem (that the outer class is not yet defined), I don't understand what...
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
3
by: DC Gringo | last post by:
I have an image control (that pulls an image off an ESRI map server): <ASP:IMAGE ID="imgZonedCountry" RUNAT="server"></ASP:IMAGE> In the code behind I am setting the ImageURL to a String value...
2
by: Mehdi | last post by:
Hi, I need to pass an URL via a hidden value as follow: <input type="hidden" id="Test" runat="Server"> and on Page_Load I assign a value to this hidden input as follow: Test.Value =...
6
by: Rich | last post by:
Hello, I have to create a table in an Access mdb (remotely) on the fly. Create Table tbl1(fld1 Integer, fld2 varchar(10), fld3...) Then I have to insert data: Insert Into tbl1 Values(" &...
1
by: Amith | last post by:
Here we are facing a problem which is when we pass a parameter to javascript putting '&' . Ex: &contact. In Japanese OS when we receive this parameter in the script '&' is converted to 'e'.So...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.