Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with LPTSTR...

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hallo,

I have a problem with the following source:

typedef struct _TEST1 {
LPTSTR lpStr1;
DWORD cbStr1;
} TEST1, FAR *LPTEST1; LPTEST1 lpTest
LPTSTR RetVal RetVal = "...";
strcpy( lpTest->lpStr1, RetVal);
lpTest->cbStr1= strlen (RetVal) * sizeof(BYTE);

What's wrong with it, because the debugger jumps to:
-> strcpy( lpTest->lpStr1, RetVal);
with Access Violation at 0xC0000005

and if I run the application:

Access Violation in
KRNL386.EXE at 0001:00008614.
Register:
EAX=00004c00 CS=013f EIP=00008614 EFLGS=00010212
EBX=00558d03 SS=0167 ESP=0065fb0a EBP=0065fb0e
ECX=cccccccc DS=0157 ESI=818740b8 FS=558f
EDX=00780c10 ES=0167 EDI=0065fdf8 GS=0000
Bytes at CS:EIP:
87 5e fc 8e 5e fe 50 9c 58 f6 c4 02 58 75 01 fb
...

Bye Tim
 
LPTSTR is a pointer... you need to allocate memory for it first.

Matt
 
first, you should know your lpTest is a point of struct _TEST1. You have to allocate memory for lpTest. Then you have to allocate memory for lpTest->lpStr1.
 
Hallo,

but if I do it this way:

typedef struct _TEST1 {
LPTSTR lpStr1;
DWORD cbStr1;
} TEST1, FAR *LPTEST1;
LPTEST1 lpTest;
LPTSTR RetVal;

RetVal = "...";
lpTest = (LPTEST1)malloc(sizeof(LPTEST1));

strcpy(lpTest->lpStr1, RetVal);
lpTest->cbStr1= strlen (RetVal) * sizeof(BYTE);

I get an error, as well (debugger shows this):

jmp short main_loop ; taken if bits 24-30 are clear and bit
; 31 is set
byte_3:
-> mov [edi],edx
mov eax,[esp+8] ; return in eax pointer to dest string
pop edi
ret
byte_2:
mov [edi],dx


What's wrong?

Bye Tim
 
try to add
lpTest->lpStr1 = (LPTSTR) malloc(20);
before strcpy. Because you have to allocate the momory for lpStr1 too.
 
Now, i get following error:

main_loop: ; edx contains first dword of sorc string
mov [edi],edx ; store one more dword
add edi,4 ; kick dest pointer
main_loop_entrance:
mov edx,7efefeffh
->mov eax,dword ptr [ecx] ; read 4 bytes

add edx,eax
xor eax,-1

xor eax,edx
mov edx,[ecx] ; it's in cache now
 
typedef struct _TEST1 {
LPTSTR lpStr1;
DWORD cbStr1;
} TEST1, FAR *LPTEST1;

LPTEST1 lpTest;
LPTSTR RetVal;


RetVal = "...";
lpTest = (LPTEST1)malloc(sizeof(LPTEST1));
lpTest->lpStr1 = (LPTSTR) malloc(20);

strcpy(lpTest->lpStr1, RetVal);
lpTest->cbStr1= strlen (RetVal) * sizeof(BYTE);



The whole thing looks like this:

### Delphi-DLL: ###
// or PAnsiChar?
function StartSth (Param1: integer): PChar; export; stdcall;
begin
if Param1= 123 then
begin
...
end;
end;


### Netspi.h ####
...
typedef struct _LOGONINFO {
LPTSTR lpUsername; // LPCSTR??
LPTSTR lpPassword;
DWORD cbUsername;
DWORD cbPassword;
} LOGONINFO, FAR *LPLOGONINFO;

typedef SPIENTRY F_NPLogon(
HWND hwndOwner,
LPLOGONINFO lpAuthentInfo,
LPLOGONINFO lpPreviousAuthentInfo,
LPTSTR lpLogonScript,
DWORD dwBufferSize,
DWORD dwFlags
);

F_NPLogon NPLogon;
typedef F_NPLogon FAR *PF_NPLogon;
...


### C++ program: ###

typedef LPTSTR (CALLBACK* LPFNDLLFUNC1)(int val); // LPC?

SPIENTRY NPLogon( HWND hwndOwner,
LPLOGONINFO lpAuthentInfo,
LPLOGONINFO lpPreviousAuthentInfo,
LPTSTR lpLogonScript,
DWORD dwBufferSize,
DWORD dwFlags )
{
HINSTANCE HDLL;
LPTSTR RetVal; // LPCSTR
LPFNDLLFUNC1 lpfnDllFunc1;


/* Copy the user credentials into the LOGONINFO structure
This information will be passed back to MPR which will
be passed as input to all the subsequent network
providers. */

HDLL = LoadLibrary(WLDir);
if (HDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(HDLL,
"StartSth"); // Name
if (!lpfnDllFunc1)
{
MessageBox(NULL, "ERROR!",
GetCommandLine(),
MB_OK | MB_ICONERROR);
FreeLibrary(HDLL);
return 110;
}
else
{
RetVal = lpfnDllFunc1(123);
}
}

FreeLibrary (HDLL);
strcpy( lpAuthentInfo->lpUsername, RetVal);
strcpy( lpAuthentInfo->lpPassword, "Jmn2837jmn");

lpAuthentInfo->cbUsername = strlen (RetVal)* sizeof(BYTE);
lpAuthentInfo->cbPassword = 10 * sizeof(BYTE);
...
}
 
I don't know if you still have problem in your test program or in your original program.
I didn't see any problem in your test program.
But in your orginal program, depending on how you call the NPLogon function, If you didn't allocate the memory for lpAuthentInfo instance properly before you call the function, you certainly will get the error.
 
Hallo,

the thing is, that the original MS-DDK example looked like this:

SPIENTRY NPLogon( HWND hwndOwner,
LPLOGONINFO lpAuthentInfo,
LPLOGONINFO lpPreviousAuthentInfo,
LPTSTR lpLogonScript,
DWORD dwBufferSize,
DWORD dwFlags )
{
switch ( dwFlags )
{
case LOGON_PRIMARY:
/* Copy the user credentials into the LOGONINFO
structure This information will be passed back to
MPR which will be passed as input to all the
subsequent network providers. */

strcpy( lpAuthentInfo->lpUsername, "murtuzan");
strcpy( lpAuthentInfo->lpPassword, "Jmn2837jmn");

lpAuthentInfo->cbUsername= 8 * sizeof(BYTE);
lpAuthentInfo->cbPassword = 10 * sizeof(BYTE);

break;
default:
OutputDebugString ( " Default flags\n");
}

return WN_SUCCESS;
}



and it works, but the following doesn't work:

LPTSTR RetVal;

...
RetVal = lpfnDllFunc1(21022002);
...

strcpy( lpAuthentInfo->lpUsername, RetVal);
strcpy( lpAuthentInfo->lpPassword, "Jmn2837jmn");

lpAuthentInfo->cbUsername = strlen (RetVal)* sizeof(BYTE);
lpAuthentInfo->cbPassword = 10 * sizeof(BYTE);

Why not?
 
if you can cofirm strcpy( lpAuthentInfo->lpUsername, "murtuzan"); works but strcpy( lpAuthentInfo->lpUsername, RetVal); doesn't work, I am sure there is some problem with RetVal. Check if you get the return value of the function properly.

 
Hallo,

the problem was FreeLibrary-> it deleted the content of RetVal so there were no value in it anymore and made some errors.

So thank you for your help.

Bye Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top