|
Well, even though as others as pointed out, this is not good code... One easy answer is simply that each client code append say a block of a given length before the data for identification purpose (assuming this is for educative purpose).
Philippe Mori
|
|
|
|
|
Context for the solution helps. So is this a commercial solution, something for school, experimenting, etc.
As others have noted there are standard idioms that you can research for doing client/server setups.
If you want to roll you own then
1. Separate out communication from processing. Different classes. So you need classes that 'send' a message and classes that 'receive' a message. Then different classes that package a 'desktop image' as a message
2. On the server side when a server accepts a client connection then the server spins up a new thread to handle that request (for messages between the original client and the server.) It does that for each client and perhaps for each message that a unique client sends. A quick glance at your code suggests that you are not creating a new thread for each request.
|
|
|
|
|
I am aware of two "fake data" generators for C#, Faker and Faker.NET, and I have posted a message with information about them on the "Free Tools" forum today: [^].
These generators are both libraries of static functions that return strings. Faker.Net (available through NuGet) has some interesting coding techniques.
I am curious if anyone knows of a generator that spits out DataTables.
thanks, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
BillWoodruff wrote: I am curious if anyone knows of a generator that spits out DataTables. Shouldn't be too hard to build..
Then again, what use would an app be that generates random tables with random relations? Makes more sense to fill an existing table-structure with fake or random data.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
I have a need for some huge auto-generated DataTables ... for stress-testing a UI generator... and, indeed, I am going to write one, and have started writing one, but wanted to see if there was one already out there. Right now, I am using Faker.Net for a random-data-by-Type provider, and building on top of that.
Being free of life$tyle constraints enables me to pursue such esoteric frivolities.
cheers, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
modified 22-Jan-16 21:25pm.
|
|
|
|
|
BillWoodruff wrote: stress-testing Wouldn't call that frivolities
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have already asked this question on the MSDN Community, but I haven't gotten an answer yet, so I hope the people here can help me.
As a little test project I wrote an ICustomMarshaler, but it's not behaving as I expected. The complete project can be downloaded as zip from my OneDrive.
I'm trying to work with 2 functions from Crypt32.dll which I import in my code:
[DllImport("Crypt32.dll", SetLastError = true)]
private static extern bool CryptProtectData([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler))] byte[] pDataIn, [Optional] string szDataDescr, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler)), Optional] byte[] pOptionalEntropy, IntPtr pvReserved, [Optional] IntPtr pPromptStruct, int dwFlags, [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler)), Out] byte[] pDataOut);
[DllImport("Crypt32.dll", SetLastError = true)]
private static extern bool CryptUnprotectData([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler))] byte[] pDataIn, [Optional] string ppszDataDescr, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler)), Optional] byte[] pOptionalEntropy, IntPtr pvReserved, [Optional] IntPtr pPromptStruct, int dwFlags, [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ByteArrayToDataBlobMarshaler)), Out] byte[] pDataOut);
Marshaling into native code works fine, marshaling back seems to work too when using the debugger to step through the MarshalNativeToManaged method:
public object MarshalNativeToManaged(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero) { return null; }
int length = Marshal.ReadInt32(pNativeData);
byte[] data = new byte[length];
IntPtr dataPtr = Marshal.ReadIntPtr(pNativeData, IntPtr.Size);
Marshal.Copy(dataPtr, data, 0, length);
return data;
}
When arriving at the 'return data;' point, the array actually has the correct data in it.
The data is called from the following code:
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
byte[] cipherBytes = new byte[0];
if (CryptProtectData(clearBytes, null, null, IntPtr.Zero, IntPtr.Zero, 0, cipherBytes))
After the call to this function, cipherBytes is still an empty array. According to standard C# rules this is to be expected, as i'm not sending cipherBytes as ref or out. So why do I actually expect it to be updated? Because the Marshal class can also do it.
I rewrote the code a little bit and used a class with StructLayout.Sequential and sent it to the native code, no matter how I send the parameters, Marshal completely ignores if it's an In, Out or InOut parameter and Always reads pDataIn, and always overwrites pDataOut, I expected the same from ICustomMarshaler.
The imports for sending it as a struct have errors in them, intentional, as I was trying to see how the In and Out attributes are used when marshaling as UnmanagedType.LPStruct. Turns out they're ignored.
[DllImport("Crypt32.dll", SetLastError = true, EntryPoint = "CryptProtectData")]
private static extern bool CryptProtectData4([MarshalAs(UnmanagedType.LPStruct), Out] DataBlob pDataIn, [Optional] string szDataDescr, [MarshalAs(UnmanagedType.LPStruct), Optional] DataBlob pOptionalEntropy, IntPtr pvReserved, [Optional] IntPtr pPromptStruct, int dwFlags, [MarshalAs(UnmanagedType.LPStruct), Out] DataBlob pDataOut);
[DllImport("Crypt32.dll", SetLastError = true, EntryPoint = "CryptUnprotectData")]
private static extern bool CryptUnprotectData4([MarshalAs(UnmanagedType.LPStruct), Out] DataBlob pDataIn, [Optional] string ppszDataDescr, [MarshalAs(UnmanagedType.LPStruct), Optional] DataBlob pOptionalEntropy, IntPtr pvReserved, [Optional] IntPtr pPromptStruct, int dwFlags, [MarshalAs(UnmanagedType.LPStruct), Out] DataBlob pDataOut);
The class is actually longer, has a nice destructor for cleaning up the unmanaged memory and changing the data in the array, but trying to keep the snippets short.
[StructLayout(LayoutKind.Sequential)]
internal class DataBlob
{
[MarshalAs(UnmanagedType.U4)]
private int Length;
private IntPtr Data;
}
The code calling it:
string clearText = "TestString";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
DataBlob clearBlob = new DataBlob(clearBytes);
DataBlob cipherBlob = new DataBlob();
CryptProtectData4(clearBlob, null, null, IntPtr.Zero, IntPtr.Zero, 0, cipherBlob);
byte[] cipherBytes = cipherBlob.GetData();
Even when sending pDataOut decorated with an In attribute, this code manages to change my private fields (not properties), so why can't my ICustomMarshaler change stuff?
|
|
|
|
|
Hi,
I have been using P/Invoke for many years (and wrote a few articles on it), but I always stayed well away from complex marshaling, so I'm no expert on that whatsoever.
Nonetheless, I am confident that the C# compiler seeing these two lines in a method:
byte[] cipherBytes = new byte[0];
if (CryptProtectData(clearBytes, ..., cipherBytes))...
will always consider cipherBytes as a zero-length byte array since you didn't tell CryptProtectData is allowed to modify it, so it will be passed by value (which for an object is an address, not a pointer to an address). Whatever other code does can't change that.
|
|
|
|
|
I have to agree on that according to the normal calling conventions in C#. But as said, the Marshal class can change private fields in a class marshaled as LPStruct. Seeing this I hoped that using the Marshal class with an ICustomMarshaler I had the same options. Guess not.
I'm guessing that's because of unsafe pointers to the private members which are both blittable in my case. I can imagine Microsoft doesn't want random programmers to do those crazy things, and the array I'm trying to Marshal as an unmanaged struct is far from blittable.
|
|
|
|
|
I'm afraid that is too much hoping and guessing; the C# language is clearly defined, and the C# implements what is in the specification.
|
|
|
|
|
Yes, but Marshal clearly breaks those rules when updating private fields from outside the class. That's why I was hoping Marshal would also break them here. I'll work with the class/struct way.
|
|
|
|
|
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
|
|
|
|
|