Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SharedMemory, create filemapping path
#1
Below I have a macro "A" and a function "B".
B needs to output "TEST", which it does.
But if I change the the path in the 'create' command to "GlobalXYZ", B outputs empty an line.
Is "Global" a required string for this to work?

Macro A
Code:
Copy      Help
__SharedMemory sm1
lpstr m=sm1.Create("Global\123" 1000) ;; 'Global' required?
str r="TEST"
int dwMemSize=1000
lstrcpyn m r dwMemSize
B

Function B
Code:
Copy      Help
__SharedMemory sm2
lpstr m=sm2.Open("Global\123")
out m


Within QM there is another example
Code:
Copy      Help
assume this code is running in process A
__SharedMemory sm1
byte* m1=sm1.Create("QM test" 4096)
m1[0]=5

assume this code is running in process B
__SharedMemory sm2
byte* m2=sm2.Open("QM test")
out m2[0]

As soon as I add a ""\" backslash within "QM test", for example "QM\test" the example doesnt work either. Does this mean that you can use:
- any string without backslashes?
- or a path BUT it must begin with "Global"?

Note that I change the file mapping paths in BOTH items (A and B, also in the QM example).
#2
Quote:As soon as I add a ""\" backslash within "QM test", for example "QM\test" the example doesnt work either. Does this mean that you can use:
- any string without backslashes?
- or a path BUT it must begin with "Global"?

Yes. If "Global\Name", the object is the same in all user sessions. If "Name" or "Local\Name", each user session can have its own object instance with same name.
#3
AAH! That makes it clear!
Thank you!
#4
Is it ok to for "dwMemSize" to get it's value (memory size) like this:

Macro sm_A_example2
Code:
Copy      Help
int dwMemSize=sizeof(r)
dwMemSize=dwMemSize+100 ;; Add a small amount...

I always add 100 to create extra buffer just in case.

I read the information about "lstrcpyn" here:
https://msdn.microsoft.com/en-us/librar ... 85%29.aspx
Below it is copied and pasted.

Quote:
Code:
Copy      Help
LPTSTR WINAPI lstrcpyn(
  _Out_ LPTSTR  lpString1,
  _In_  LPCTSTR lpString2,
  _In_  int     iMaxLength
);

iMaxLength [in]
Type: int
The number of TCHAR values to be copied from the string pointed to by lpString2 into the buffer pointed to by lpString1, including a terminating null character.


Return value

Type: LPTSTR

If the function succeeds, the return value is a pointer to the buffer. The function can succeed even if the source string is greater than iMaxLength characters.

If the function fails, the return value is NULL and lpString1 may not be null-terminated.

Remarks

The buffer pointed to by lpString1 must be large enough to include a terminating null character, and the string length value specified by iMaxLength includes room for a terminating null character.

The lstrcpyn function has an undefined behavior if source and destination buffers overlap.
Security Warning

Using this function incorrectly can compromise the security of your application. This function uses structured exception handling (SEH) to catch access violations and other errors. When this function catches SEH errors, it returns NULL without null-terminating the string and without notifying the caller of the error. The caller is not safe to assume that insufficient space is the error condition.

If the buffer pointed to by lpString1 is not large enough to contain the copied string, a buffer overrun can occur. When copying an entire string, note that sizeof returns the number of bytes. For example, if lpString1 points to a buffer szString1 which is declared as TCHAR szString[100], then sizeof(szString1) gives the size of the buffer in bytes rather than WCHAR, which could lead to a buffer overflow for the Unicode version of the function.

Buffer overflow situations are the cause of many security problems in applications and can cause a denial of service attack against the application if an access violation occurs. In the worst case, a buffer overrun may allow an attacker to inject executable code into your process, especially if lpString1 is a stack-based buffer.

Using sizeof(szString1)/sizeof(szString1[0]) gives the proper size of the buffer.

Consider using StringCchCopy instead; use either StringCchCopy(buffer, sizeof(buffer)/sizeof(buffer[0]), src);, being aware that buffer must not be a pointer or use StringCchCopy(buffer, ARRAYSIZE(buffer), src);, being aware that, when copying to a pointer, the caller is responsible for passing in the size of the pointed-to memory in characters.
#5
No.
str consists of 2 parts:
Fixed-size part containing pointer to string, length etc. It is 16 bytes, and sizeof gets it. Not used with process memory.
Variable-size string memory, which is r.len+1+extra. You need r.len+1.

lstrcpyn is useful to copy a string of unknown length to a fixed-size memory. Copies whole or part of string.

In C/C++ sizeof also can be used to get string constant length. In QM no.
#6
OK!
Thank you!!
EDIT: Yes! it works perfectly now, I had issues of strings getting cut off. Now it always outputs correct!


Forum Jump:


Users browsing this thread: 1 Guest(s)