|
It needs an out or ref to do its job, and then the compiler knows the calling method is to expect an object is going to be substituted. That is what those keywords are for, no rules are broken.
|
|
|
|
|
It gets replaced? That's something I missed. That makes a lot more sense. Personally I thought it was changing the object I sent in. That explains instantly why CleanUpManagedData gets called for ref instances and not for In parameters.
It's still cheating on creating the new object, as it's not using my constructors, but I can see how Microsoft can just recreate the object by writing the fields straight in to memory.
I have tried more and the last thing that bothers me: when I do send an object as ref, the IntPtr my MarshalManagedToNative creates also gets sent as ref. Is it possible to tell ICustomMarshaler "The managed object I send in is ref, but the IntPtr I create needs to be sent as is"?
--edit-- In Out error
modified 23-Jan-16 6:03am.
|
|
|
|
|
1.
The MSDN C# language reference pages such as out[^] and ref[^] explain it well; in particular the last example on ref could interest you.
2.
You can apply out or ref on any type you like, personally I haven't ever seen or used it on IntPtr . IntPtr is typically used to pass the address of something (a managed object, an unmanaged block of memory) that already exists (or is allocated) of which you do not want to pass the content at the same time.
3.
I recommend you follow what was put forward in the article Gerry provided a link to.
|
|
|
|
|
1. Thanks for the links.
3. Already done as I replied to Gerry.
2. I don't want to make ref IntPtr, Marshal does that and I don't want it.
When using my ICustomMarshaler and sending pDataOut as normal, so no ref or out, the MarshalNativeToManaged works fine, just the result doesn't get used because I don't send pDataOut as ref or out.
If you then change the pDataOut to a ref, the MarshalNativeToManaged fails. Why? Because the IntPtr coming in to the method actually contains the dwLength, the amount of bytes, the first field of the struct. This can only happen when Marshal sends the IntPtr as ref, and the native code expects a pointer to the struct, not a pointer to the pointer. So it overwrites the IntPtr with the first field, and it overwrites the next 4 or 8 bytes (32 or 64 bit) with the pointer to the bytes. This last thing is actually memory corruption.
|
|
|
|
|
|
I have seen that page, and in my opening post you can see I use a class DataBlob which actually works exactly as that article describes.
I just changed it to a class so I can use a destructor. The reason I wanted an ICustomMarshaler is that I was using this as a testproject and I also wanted to implement IDisposable correctly. The problem that arose was that I couldn't use a bool IsDisposed as the Marshaler also marshals that boolean into the unmanaged struct, which is very unwanted. Sadly there isn't an attribute which you can use which tells the marshaler "ignore this field".
ICustomMarshaler keeps all the unmanaged memory in the hands of the ICustomMarshaler, and also cleans it up. No need for a destructor or dispose, and I can choose myself which fields I want to bring over and which not.
|
|
|
|
|
Currently I am having one url with www.domainname/countrycode
1.at present my countrycode is default lets say Uk.
2.my requirment is according to my country location my url should append with countrycode.
Ex: if I type url from India ---> www.domainname.com/in
from UK --> www.domainname.com/uk
modified 21-Jan-16 9:15am.
|
|
|
|
|
Think about this for a second. Just how is the URL the user is typing supposed to change when your code isn't even running in the browser yet because they haven't gotten to your page yet because they're STILL TYPING THE URL?!
|
|
|
|
|
... also, please do not post the same question in multiple forums; it just duplicates work.
|
|
|
|
|
Get the IP address. Translate the address to a country:
IP Location Finder - Geolocation[^]
Append the "country code" to the input.
(Whether this happens client or server-side depends on the view states).
|
|
|
|
|
Dears,
I have a table that contain country name + phone code and another table of called phone number.
what is the way to know the phone calls destinations using the first table that contains the codes and country name
example
table 1: 961 | Lebanon
table 2: 9613211311 => Lebnon
|
|
|
|
|
SELECT table1.Country FROM table1 JOIN table2 ON table1.Code = MID(table2.PhoneNumber, 1, 3) WHERE table2.PhoneNumber = '9613211311';
I may have misused the MID function, but you get the point.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
thanks, the codes are variable, they may be 1 char such as USA code is 1 and reaches 9 chars such as India code,
the dialed numbers in the other table are to many countries
|
|
|
|
|
So something like this:
SELECT TOP 1
Country
FROM
CountryCodes
WHERE
@PhoneNumber Like Code + '%'
ORDER BY
Len(Code) DESC
;
That will give you the country for a single phone number passed in as a parameter. If there are multiple matching country codes, it will return the country for the longest matching code.
If you want the country for multiple phone numbers, try:
WITH cteCodes As
(
SELECT
CountryCodes.Country,
PhoneNumbers.PhoneNumber,
ROW_NUMBER() OVER (PARTITION BY PhoneNumbers.PhoneNumber ORDER BY Len(CountryCodes.Code) DESC) As RN
FROM
CountryCodes
INNER JOIN PhoneNumbers
ON PhoneNumbers.PhoneNumber Like CountryCodes.Code + '%'
)
SELECT
Country,
PhoneNumber
FROM
cteCodes
WHERE
RN = 1
;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Maybe an INNER JOIN ON a LIKE ? Which database system?
|
|
|
|
|
|
Next time, please post a database question in the database forum
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
So you want to do this matching in your C# app and not with SQL?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
(Why didn't you tell Richard Andrew?)
Then what type are the tables you have there? System.Data.DataTable ?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Be sure that you validate that the numbers in the second table are in the form that you expect.
|
|
|
|
|
Disclaimer: This is not a question asking for help with programming, more of a short survey.
Now that we have string interpolation built into C# 6, do you still see a use for String.Format or for some custom String.Format-replacement or prefer one or the other to the inbuilt interpolation? (And if so, why?)
Do you have a project where fast string formatting is critical?
///
I wrote a String.Format-replacement before C# 6 came out and I've started writing an article on that. Even if there might be no actual need for it any more, it still makes a really good example for using compiled expressions (which is why my solution is blazing fast for the second and following uses of the same format string). Getting some more opinions on this might be useful for the introduction / the way I'll present it.
cheers,
Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
For a lot of people, upgrading to the latest version of C# will be a prohibited move due to workspace constraints. I would imagine that any display of alternatives would be a welcome addition for them.
This space for rent
|
|
|
|
|
Pete O'Hanlon wrote: For a lot of people, upgrading to the latest version of C# will be a prohibited move due to workspace constraints. Thank you for that point. Being a sole developer selling my own programs in binary only I don't have to think about those issues. I expected the .NET-version would be a workspace constraint for people but didn't expect the C#-version to be one as well. VS licensing issues I guess.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|