Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / February 2006

Tip: Looking for answers? Try searching our database.

PInvok'ing methods with LPCTSTR parametesr

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ines - 24 Feb 2006 21:51 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.