|
Hi,
Sorry for this late reply but I was on vacation these days.
First, you have to make sure that generated classes are all defined with partial keyword.
For example :
public partial class DataClass
{
public partial class Email
{
}
}
Then, just add a Whatever.cs file to your project :
public partial class DataClass
{
public partial class Email
{
public override string ToString()
{
}
}
}
You could also define an extension method for you Email class : just add an Extensions.cs file to your project and define an extension method to the Email class.
public static class Extensions
{
public static string ToString(this Email email)
{
}
}
Hope this helps. Kindly.
|
|
|
|
|
Hi,
We have an issue - how to pass .net (C#) string to unmanaged C++ dll?
We tried something, but this error came up:
"An unhandled exception of type 'System.AccessViolationException' occurred in mscorlib.dll
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Does anybody know how to do this? We need the code sample.
Thank you in advance.
Goran
|
|
|
|
|
Should be no problem. A managed string can be passed to a native char* (read-only) in a straightforward way; for a writeable char*, you need a StringBuilder. See this little article[^].
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
Thank you.
I've just tried it, but the same error came up.
Our C# code looks as follows:
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("F4100FB3-75C5-4C4F-8034-9B836598DD1E")]
public interface IDecoder
{
[return: MarshalAs(UnmanagedType.BStr)]
StringBuilder Decode([MarshalAs(UnmanagedType.BStr)] ref string encodedString);
public class UfoDecoder : IDecoder
{
public StringBuilder Decode(ref string encodedString)
{
byte[] dec64 = Base64Decode(encodedString);
byte[] unzipped = CompressionUtility.Decompress(dec64);
return BinToXml(unzipped);
}
And our C++ code looks as follows:
extern "C" __declspec(dllexport) void decode(char *encString, char *answer )
{
HRESULT hr = CoInitialize(NULL);
IDecoderPtr pIDec(__uuidof(UfoDecoder));
BSTR k1 = bstr_t(encString);
BSTR k2 = bstr_t(answer);
pIDec -> Decode(&k1, &k2);
The error comes up on calling last line in C++ block.
modified on Monday, May 30, 2011 5:05 AM
|
|
|
|
|
get rid of the ref keyword, objects always get passed by reference, your native code will get a pointer without using ref .
FWIW: With ref , it would be a char** .
[ADDED]
On a second read, I don't understand your native code at all. It seems to me the decode() function isn't relevant. All that matters is Decode(), of which you didn't show the actual signature. However your prototype suggests it returns a StringBuilder (bad idea, that is a managed type, no native function returns managed types automatically), and yet your decode() just ignores the return value!?!?
If you need more help, please show the declaration of the native Decode() function.
If it doesn't require BSTR (say it just takes two char*, then things should be simple).
[/ADDED]
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
modified on Monday, May 30, 2011 5:38 AM
|
|
|
|
|
Do you mean on this:
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("F4100FB3-75C5-4C4F-8034-9B836598DD1E")]
public interface IDecoder
{
[return: MarshalAs(UnmanagedType.BStr)]
StringBuilder Decode([MarshalAs(UnmanagedType.BStr)] string encodedString);
public class UfoDecoder : IDecoder
{
public StringBuilder Decode(string encodedString)
{
byte[] dec64 = Base64Decode(encodedString);
byte[] unzipped = CompressionUtility.Decompress(dec64);
return BinToXml(unzipped);
}
If so, I have the same error.
|
|
|
|
|
sorry, we were both typing at the same time. I expanded my earlier reply.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
|
|
|
|
|
Ok. The whole code is here:
extern "C" __declspec(dllexport) void decode(char *encString, char *answer )
{
HRESULT hr = CoInitialize(NULL);
IDecoderPtr pIDec(__uuidof(UfoDecoder));
BSTR k1 = bstr_t(encString);
BSTR k2 = bstr_t(answer);
pIDec->Decode(&k1,&k2);
SysFreeString(k1);
SysFreeString(k2);
}
int _tmain(int argc, _TCHAR* argv[])
{
char *xml = (char*)malloc(5000000*sizeof(char));
char* str = "Some encoded string";
int length = strlen(str);
decode(str, xml);
printf("Decoded = %s",xml);
int vol = strlen(xml);
return 0;
}
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("F4100FB3-75C5-4C4F-8034-9B836598DD1E")]
public interface IDecoder
{
[return: MarshalAs(UnmanagedType.BStr)]
string Decode([MarshalAs(UnmanagedType.BStr)] ref string encodedString);
[ComVisible(true)]
[Guid("85F2DFFB-5655-40DD-A779-94388A2773DC")]
[ProgId("UfoDecodeWrapper.UfoDecoder")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IDecoder))]
public class UfoDecoder : IDecoder
{
public string Decode(ref string encodedString)
{
byte[] dec64 = Base64Decode(encodedString);
byte[] unzipped = CompressionUtility.Decompress(dec64);
return BinToXml(unzipped);
}
[ADDED]
If str variable in _tmain function in this line:
char* str = "Some encoded string";
is not so long we're getting Xml.
But, if str is very long, the error comes up.
[/ADDED]
modified on Monday, May 30, 2011 6:22 AM
|
|
|
|
|
Sorry, I give up. It is all just too confusing for me. I now think you have native code calling managed code, not the other way around.
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Please use <PRE> tags for code snippets, they improve readability. CP Vanity has been updated to V2.3
modified on Friday, June 10, 2011 8:47 PM
|
|
|
|
|
I've added some explanation up there:
[ADDED]
If str variable in _tmain function in this line:
char* str = "Some encoded string";
is not so long we're getting Xml.
But, if str is very long, the error comes up.
[/ADDED]
|
|
|
|
|
"decode" function is relevant because it's placed in this function:
int _tmain(int argc, _TCHAR* argv[])
{
char *xml = (char*)malloc(5000000*sizeof(char));
char* str = "Some encoded string";
int length = strlen(str);
decode(str, xml);
printf("Decoded = %s",xml);
int vol = strlen(xml);
return 0;
}
|
|
|
|
|
Please avoid using things like:
char* str = "A string literal here";
If you define it this way, it should be defined as a const char*. If it needs to be editted, define it as a char array with a large enough size.
Also, why are you calling pIDec->Decode(&k1,&k2); . Why not just pIDec->Decode(k1,k2); ? And then... why are you defining Decode as a function with one parameter and yet calling it with two parameters?
|
|
|
|
|
As Luc Says, it shoul be no problem. To help any further we would need to see the unmanaged function signature.
|
|
|
|
|
Yes. I've just posted it up there.
|
|
|
|
|
Hallo
my application sports a dgv bound to a datatable, which has a column with default value to DateTime.Now.
When the user add a new row, that datetime column is correctly filled; when the user saves for the first time, it correctly saves. Any next time the user try saving the row, it gives a System.FormatException "String not recognized as valid DateTime value", and stops saving.
The DateTime in the DGV appears to be formatted according the locale Culture (italian, dd/MM/yyyy hh.mm)
The DataTable.Locale is System.Globalization.CultureInfo.InvariantCulture.
If I change it to System.Globalization.CultureInfo.CurrentCulture i get an obscure exception "Found unknown word starting at index 0".
Any help?
Thanks in advance!
|
|
|
|
|
Do you have a DateTimePicker in the DataGridViewCell or is it a default DataGridViewCell (Text) Field?
You may need to convert the value to a valid Date Type.
I don't speak Idiot - please talk slowly and clearly
'This space for rent'
Driven to the arms of Heineken by the wife
|
|
|
|
|
Hallo
the DGV is a plain one, is bound to a BindingSource which has is DataSource property set to the DataTable.
No modification is made on the fields type: the field holding the datetime is a default DataGridViewCell, automatically created at binding.
You're giving me an interesting hint... By the way I hope that I should not create manually each column in the DataGridView to have it right typed .
|
|
|
|
|
Actually playing with culture info is not good. Using the Datetime control is better.
Did u try? I used the below code. Its fine.
Sub Form_Load()
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB", False)
End Sub
Thanks & Regards
PARAMU
|
|
|
|
|
Hallo paramu, thank for the reply.
The datatable.locale is used by Microsft in many examples, I kept it for preventing any issue from handling italian characters like accents, and for datetime handling too.
I tried your code but it didn't work, neither with "en-GB" nor with "it-IT".
It sounds that this is happening:
1. the default value in datatable is correct
2. the datagridview format it as string
3. when saving, the system fails to handle the string.
I would like to try to explicitly type the datetime column, but this would require me to manually code each column of the DGV.
Ubi maior, minor cessat... I'll give it a try.
|
|
|
|
|
While saving time, before to udate to your sever, it has to be
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US", False)
And while save finish it has to be again
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB", False)
Also form_load()
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB", False)
Thanks & Regards
PARAMU
|
|
|
|
|
thanks it doesn't work.
I'll try dgv.autogeneratecolumns = false and so on.
|
|
|
|
|
I'm giving up, it's quite a nightmare if the system itself doesn't understand values it automatically wrote.
|
|
|
|
|
LordZoster wrote: it's quite a nightmare if the system itself doesn't understand values it automatically wrote
This is why I nearly always roll my own DAL and handle the databinding with custom business objects.
I never use the BindingSource stuff..
I don't speak Idiot - please talk slowly and clearly
'This space for rent'
Driven to the arms of Heineken by the wife
|
|
|
|
|
You mean, u need to convert it from string
MyDataTable.Rows[RowNum]["MyDateTimeField"]=Convert.ToDateTime(MyStringDate);
Are you talking about this?
|
|
|
|
|
The BindingSource is supposed to handle this conversion by itself, doesn't it?
And, in case I overrun this conversion writing my own one, where should I write it?
From what I understand (very little, I admit) I should overrun a whole lot of stuff of BindingSource: in this I totally agree with Andy_J, it is preferrable to use one's own DAL.
The worst thing is, that datetime cell is readonly, so there's no need to write it back into the database from the datagridview hence no need to convert any string...
|
|
|
|