Hey everyone,
I am running into a problem PInvoking a particular method I expose in a
DLL, that basically configures and starts an ETW session
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/co
nsuming_events.asp).
The basic method looks like this:
__declspec(dllexport) HResult SartETWTrace(TraceHandle* traceHandle,
LPCTSTR logSessionName, LPCTSTR logFilePath)
{
EVENT_TRACE_PROPERTIES* pProperties = NULL;
ULONG BufferSize = 0;
ULONG rc = 0;
TRACEHANDLE SessionHandle = 0;
BufferSize = sizeof(EVENT_TRACE_PROPERTIES) + sizeof(logFilePath) +
sizeof(logSessionName);
pProperties = (EVENT_TRACE_PROPERTIES*) malloc(BufferSize);
if (NULL == pProperties)
{
wprintf(L"Unable to allocate %d bytes for properties structure.\n",
BufferSize);
return S_FALSE;
}
ZeroMemory(pProperties, BufferSize);
pProperties->Wnode.BufferSize = BufferSize;
CopyMemory((PVOID)&(pProperties->Wnode.Guid), &MyTraceSession,
sizeof(GUID));
pProperties->LoggerNameOffset = sizeof(EVENT_TRACE_PROPERTIES);
pProperties->LogFileNameOffset = sizeof(EVENT_TRACE_PROPERTIES) +
sizeof(logSessionName);
wcscpy((LPWSTR)((char*)pProperties + pProperties->LogFileNameOffset),
logFilePath);
rc = StartTrace((PTRACEHANDLE)&SessionHandle, logSessionName,
pProperties);
if (ERROR_SUCCESS != rc)
{
wprintf(L"StartTrace() failed, %d\n", rc);
return S_FALSE;
}
free(pProperties);
pProperties = NULL;
*traceHandle = SessionHandle;
return S_OK;
}
>From C#, I Pinvoke as follows:
[DllImport("ETWWrapper.dll")]
public static extern void SartETWTrace([Out] out ulong sessionHandle,
[In] string sessionName, [In] string logFilePath);
string sessionName = "sessionName";
string logFilePath = "C:\someLog.etl";
ulong sessionHandle;
SartETWTrace(out sessionHandle, sessionName, sessionHandle);
The method fails because StartTrace returns ERROR_BAD_LENGTH, which
means the size of the created properties structure is properly off.
This works fine when I hard code and define the paths in the C++ DLL
itself, but the properties node seem to be improperly created and
initialized when I pass in the strings from the managed land.
Any help/suggestions on this problem would be greatly appreciated!
Thanks
Ines
Mattias Sjögren - 27 Feb 2006 19:23 GMT
>Any help/suggestions on this problem would be greatly appreciated!
sizeof(logFilePath) returns the size of a pointer. You should use
something like lstrlen instead.
Mattias

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Ines Khelifi (Windows Live Beta) - 28 Feb 2006 18:41 GMT
Yeah, I caught that and fixed it! That's what I get for copying and pasting
:)
PInvokin'g the ETW stuff works great now, thanks!
Ines
>>Any help/suggestions on this problem would be greatly appreciated!
>
> sizeof(logFilePath) returns the size of a pointer. You should use
> something like lstrlen instead.
>
> Mattias