|
If I have a c++ app that gets a pointer to a c++ char, will I be able to read that value into a C# char from the address from the c++ app? Or are they incompatible?
|
|
|
|
|
AFAIK IntPtr is the closest you can get through managed code. If there is a method in the dll which returns char* , try getting the return value into an IntPtr and then use it.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
I can't figure out what is wrong with my c#/c++ code trying to do this...
c# Code
try
{
char c;
string s;
int i;
i = System.Convert.ToInt32(Environment.GetCommandLineArgs()[1]);
unsafe
{
char* charp;
charp = (char*)i;
c = *charp;
}
s = c.ToString();
System.IO.File.WriteAllText("C:\\users\\name\\file.txt", System.IO.File.ReadAllText("C:\\users\\name\\file.txt") + Environment.NewLine + s);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException);
}
C++ Code that launches the above and passes the pointer to a char.
int inn = &the_char;
ShellExecute(NULL,NULL,"C:\\csharpfile.exe",inn,NULL,0);
|
|
|
|
|
I cannot figure out what your code is trying to do.
Take the first command line argument and convert it to an integer.
In an unsafe block cast the integer to a character pointer and take the first character of that array.
Convert that character to a string.
Use that string for some purpose.
Did you try running this through the debugger to see what results you are getting?
|
|
|
|
|
Hi,
I have a sdk written in c++ to control an IP module from .net. I wish to port all these libraries to C#. As i have never written interops, wrappers, or even worked in c++, what are my options. I need to know what i need to know to accomplish making dll's which are compatible to c# or if there are any third party tools that can convert all dll's from C++ to C#.
Thank you.
|
|
|
|
|
If you just need to use the methods that the dll exposes in your C# code, you need to check if the dll is managed or unmanaged. If it is a managed one, add reference to it, create object and call methods.
If it is an unmanaged dll, you can use DllImport attribute. Here[^] a link that might help you.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
I have got a sample application build around the sdk, its written in VC2003.net. I can add a reference to some of the dll's and ocx files but not all.
I am sending you an email to download the sdk and a sample application within can u please get some time and see into it and tell me what i am up against, it would be really helpful.
I would be really thankfull.
Just tell me what i need to do to start.
Thankyou
|
|
|
|
|
faheemnadeem wrote: I have a sdk written in c++ to control an IP module from .net.
How that SDK exposes its functionality? Do they provide static libraries or DLLs? If they provide static libraries, better option is to create a managed DLL using C++/CLI which has the static libraries linked.
If they provide DLLs, then you can use P/Invoke to communicate with it from C#.
Best wishes,
Navaneeth
|
|
|
|
|
There are converters for this, but you need to have a very good knowledge of both C++ and C# to be capable of making the adjustments to get it working.
If the C++ code makes heavy usage of pointer arithmetic, then you are better off leaving the code in C++.
David Anton
Convert between VB, C#, C++, & Java
www.tangiblesoftwaresolutions.com
|
|
|
|
|
I thought passing an object to another object would be easy, since objects are passed by reference. Wrong.
I've managed to populate the members of an object nicely, using a series of forms for user input - that was no mean feat for me, but it works. The next step in my plan was to pass the object on to another slice of code for handling the interface to a database. If I can get that to work, it's about 75% of what the program has to do. I thought I understood that objects, as reference types, could be passed as parameters to other parts of the program, but I'm having difficulty doing so. In fact, I haven't found anything here or via Google that clearly shows anyone else trying this, so I may be completely off base.
To test my approach without getting into the database can of worms, I thought I'd test ways to do this by creating yet another form, passing the object to it, then extracting the individual members to labels for display. If that works, I can apply the same principles to a database handler module of some kind.
First I tried creating the test form with a constructor that takes one argument - my object. When I instantiated the form I passed the local object as an argument. That compiled nicely, but crashed with an unhandled null object exception. I then tried creating the form without the argument, but before displaying it, set the object member in the display form equal to the local object. Same error. I'm either trying to do something impossible, or I'm missing something important here and have no idea what it is. A partial listing follows:
Calling Form:
frmShowForm frmShowRec = new frmShowForm(Rec);
if (frmShowRec.ShowDialog(this) == DialogResult.OK)
{
frmShowRec.Hide();
frmShowRec.Dispose();
Display Form:
public partial class frmShowForm : Form
{
public frmShowForm(Recloser recshow)
{
Recloser RecShow = new Recloser();
RecShow = recshow;
label1.Text = RecShow.Serial;
this.label2.Text = RecShow.Mfr;
this.label3.Text = RecShow.Type;
this.label4.Text = RecShow.Vrated;
this.label5.Text = RecShow.BIL;
Before anyone comments, I tried the above with and without the 'this' modifier; there was no change in the error message. If either way is preferable (or necessary) I'd appreciate knowing it. This is just the latest attempt, as I've been at it all night trying different approaches to the problem and searching here and with Google. I know I could create a bunch of local variables in the calling form and pass them into the display form, but that would defeat my purpose, which is to accomplish this with as little work as possible. Typical engineer...
Thanks in advance for any assistance!
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
If you pass recshow as null and do not take care of handling that in the form's constructor, you will get the exception. I could not understand the second approach you have tried so cannot comment on that.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
I thought I was passing a populated Rec into recshow, but apparently not. Oddly, when I debug the watch window shows the correct values in the fields contained in both RecShow and recshow. The error occurs when it hits the first assignment statement attempting to fill the label variables with individual values from the Recloser object.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
If you are passing filled object, it should work.
You are not calling InitializeComponent() in the constructor. Before you use any of the controls in the form, you should call that method. Just add a call to this method in the constructor as the first line and check.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
I'll be damned... It worked.
The examples I've seen in books all omitted the default Initialize in the constructor when a custom constructor was provided, so I commented it out.
Thanks a bunch!
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Welcome.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
I guess a variable has to be initialized to a non-null value before it can be initialized to a specific value. My assignment statements were all in the constructor. That seems weird to me, but I can live with it. Thanks again...
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Roger Wright wrote: I guess a variable has to be initialized to a non-null value before it can be initialized to a specific value.
No. There is no restriction like this.
Roger Wright wrote: My assignment statements were all in the constructor. That seems weird to me, but I can live with it.
You think your problem was the recShow object was null . But since you haven't called InitializeComponents() , the controls are not instantiated and your error was there.
Best wishes,
Navaneeth
|
|
|
|
|
Roger Wright wrote: since objects are passed by reference
Objects are not passed by reference. Everything will be passed by value. But for reference types, the value will be a reference.
Roger Wright wrote: public frmShowForm(Recloser recshow) //Test page to demonstrate passing a Ref object
{
Recloser RecShow = new Recloser();
RecShow = recshow;
label1.Text = RecShow.Serial;
this.label2.Text = RecShow.Mfr;
this.label3.Text = RecShow.Type;
this.label4.Text = RecShow.Vrated;
this.label5.Text = RecShow.BIL;
You don't need Recloser RecShow = new Recloser(); here. Just use the recshow object.
Best wishes,
Navaneeth
|
|
|
|
|
Dear friends ...
i encrypted and decrypt file with CryptoStream.
if some one modified the data in the encrypted file.(edit the data with notepad)
now i decrypt the file (it is not possible... i understad that) chunk by chunk. at the last while closing the file (CryptoStream.close())
i get an error .. now i could not delete the file becase it is Used by another porcess ... and i could not close the file for deleting, because closing stream throws error.
FileStream fStreame = new FileStream(sDestinationFile, FileMode.Create);
CryptoStream cStream = new CryptoStream(fStreame, alg.CreateDecryptor(), CryptoStreamMode.Write);
try
{
int readBytes = rStream.Read(bValues, 0, nBuffersize);
cStream.Write(bValues, 0, readBytes);
}
catch(Ex)
{
}
finally
{
rStream.Close();
cStream.Close();
}
please suggest me something...
by
Joe
|
|
|
|
|
Hi,
could you post the error you get while closing the stream?
Regards
Sebastian
|
|
|
|
|
Thanks for ur replay..
"Length of the data to decrypt is invalid."
stak trace
-----------
at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at PAW_ENC.AES.DecryptFileFromPosition(String sSourceFile, String sDestinationFile, Byte[] bIV, Byte[] bkey, Int64 lPosition)
|
|
|
|
|
Well it seems you didn't post the complete code. You are closing a rstream but I couldn't find the rstream in your code. Which stream is this?
|
|
|
|
|
I have found two ways to populate a treeview from a database. The first sets up a new class for each hierarchal level. Each class then contains a list of the associated child nodes. The database is queried once, then the records are dumped into the instances of the classes. This is the method used here:
Populating a TreeView Control from the Database[^]
The second way I found of populating the treeview is to have a number of methods that query the database. Each query includes filters the data by the value of the parent node, such as here:
http://www.dotnetspider.com/resources/28676-Populate-TreeView-From-DataBase-Using-Recursive.aspx[^]
My question is which way is preferable? I have a large about of data that I want work with, say, a hundred parent nodes that will drill down eight to ten hierarchal levels. It seems creating instances of a class for each record would use a lot of memory, but the second option has a lot of back-and-forth from the database. The data is coming from an oracle database, if that matters any.
|
|
|
|
|
Are you wanting to initially add EVERY node in the treeview or just the parent nodes? In a large record set, it is not wise to add all the child nodes at the beginning.
If my answer has helped you, one of my articles may also be a help. Also remember that your best friend's name is google.
|
|
|
|
|
I thought that if I loaded everything up front, then the user would not be slowed down by the the constant updating of the treeview, but I will try you suggestion. Also, I did use Google, that's how I got the information that I posted in the question.
|
|
|
|