|
I understand that, but what value are you assigning to it?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
|
|
|
|
|
I wrote a DLL with VC6.0. I want to pass a LPCSTR type parameter (file path) to a thread. Here is the code:
DWORD WINAPI ThreadFunc(LPVOID lpParam)
{
char* fileLoct = (char*)lpParam;
MessageBox(NULL, fileLoct, "Message", MB_OK |
MB_ICONINFORMATION);
return 0;
}
void __stdcall StartThread(LPCSTR flt)
{
HANDLE hThread;
hThread = CreateThread(NULL, 0, ThreadFunc,
(void *)flt, 0, NULL);
CloseHandle(hThread);
}
I tested it with MessageBox and found the path was not correct at all. How to correctly pass a LPCSTR type parameter to a thread in DLL?
Thank you.
|
|
|
|
|
What was "not correct" about it?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
MessageBox display is not file path, instead it showed something "s" or "c", "7". this is what I don't understand.
|
|
|
|
|
Are you calling the thread from a Unicode function? If you're passing a Unicode string to a function that expects an ANSI string, this is what you'll see.
See: Converting Unicode and ANSI Strings[^]
and: WideCharToMultiByte function[^]
The difficult we do right away...
...the impossible takes slightly longer.
modified 1-Mar-15 20:36pm.
|
|
|
|
|
No, my function is not a Unicode function. my file path is C:\Downloads\HornSound.wav.
I found if I added the following " MessageBox(NULL, loct, "Message1", NULL); " before "CloseHandle(hThread);".
Both MessageBoxes (both in the function and in the thread) can correctly display the file path.
But if I removed "MessageBox" in the function, then the MessageBox in the thread cannot correctly display the file path. What is the problem?
|
|
|
|
|
What is thae value of flt at the point that you call the thread, where does it come from?
|
|
|
|
|
"flt" is the file path from VBA. it's "C:\Downloads\HornSound.wav."
Sorry, it's my mistake. the "loct" in MessageBox should be replaced with "flt".
I spent some time try to figure it out.
So as I said, I added " MessageBox(NULL, flt, "Message1", NULL); " before "CloseHandle(hThread);". both messagebox can correctly display my file
path "C:\Downloads\HornSound.wav."
I also found if I replaced "MessageBox" with "Sleep(3000)" before "CloseHandle(hThread);" MessageBox in thread can correctly display my file
path "C:\Downloads\HornSound.wav."
if I add nothing and just removed "CloseHandle(hThread);", MessageBox in thread can also correctly display my file path "C:\Downloads\HornSound.wav."
It seems I cannot close handle right after CreateThread. but that can cause memory leak?
|
|
|
|
|
The fact that it works when you add a delay suggests that the file path buffer is getting destroyed before the thread process has constructed the message box. You should ensure the buffer is preserved until the thread has finished using it, by some form of synchronisation. Or better still don't use threads unless they are serving some necessary function.
|
|
|
|
|
What doesn't work? compiler error? What's the error? Runtime error? Describe it.
Give people something to go on!
Steve
|
|
|
|
|
no compiler error. when I calling the DLL from VBA, the function just cannot pass a file path to the thread. File path in thread displayed as "s", "c", etc.
|
|
|
|
|
You are using an automatic variable sent from VB call. That variable, and the string contente, will be destroyed after the function that create the thread returns. The thread instead will be running and looking for the string at the address that you passed in the thread creation, but that address holds only garbage by then...
To make it work create a local string in your code and define it 'static', copy the passed string there, then create the thread.
void __stdcall StartThread(LPCSTR flt)
{
HANDLE hThread;
static char *szStaticString[MAX_PATH];
strncpy(szStaticString, flt, MAX_PATH-1);
hThread = CreateThread(NULL, 0, ThreadFunc,
(void *)szStaticString, 0, NULL);
CloseHandle(hThread);
}
|
|
|
|
|
I didn't realize that string variable was destroyed after the function creating the thread returns. Now my DLL works great. Frankie, thank you so much.
|
|
|
|
|
You're welcome.
If you have to start more threads with different strings, I suggest to allocate dynamic memory for each string to make it local for that thread. The thread can release the memory when it doesn't need it anymore.
|
|
|
|
|
I want to covert following program into c++
subroutine gstep
common/blk1/u(3,800,800),fnl(3,800,800),gnl(3,800,800)
common/blk2/ni,nj,radi,rado,dt,cfl,lod,dto,wn,wl,lexp,dtot,ndiv,pi,phi
c in case a global time step is reqd. eg. time accurate problems
em=1.
ep=1.
c=1/sqrt(ep*em)
dt=10000.
cl=ar/diag
do i=1,ni
do j=4,nj-3
call grid(i,j,x1,y1,x2,y2,x3,y3,x4,y4,xp1,yp1,ar,c,0)
diag=0.5*(sqrt((x1-x3)**2+(y1-y3)**2)+sqrt((x2-x4)**2
* +(y2-y4)**2))
cl=ar/diag
dtemp=cfl*cl/(c)
if(dtemp.lt.dt)then
dt=dtemp
endif
enddo
enddo
wl=2*pi/wn
write(*,*) wn,wl,dt
ndiv=wl/(c*dt)
write(*,*) ndiv
ndiv=ndiv+1
dt=wl/(c*(ndiv))
write(*,*) ndiv,dt
return
end
|
|
|
|
|
|
i have to calculate diagonal using grid points
|
|
|
|
|
No, I mean what is the programming problem? Telling us what you want to do does not give enough information about where you are stuck or what you have tried. If you know the mathematics of the problem then converting that into C++ code should not be too difficult.
|
|
|
|
|
I am writing cfd code taking reference of FORTRAN code following is the code which i had written but this program is not running
#include<math.h>
#include<fstream>
#include<iomanip>
//#include "constant1.h"
using namespace std;
float radi=11,rado=59.99,dro;
float r=11,spar=0.9415,pi=3.141592654,cfl=0.50,wn=2*pi*0.6/radi;
int i=1,ni=200,nj=50,nth=ni,nrad=nj,ncon1=0,ncon2=0,ncon=1000,lod=3,lexp=2,eps=0;
//float wn;
static int j=1;
class project
{
public:
float x,y,r,th;
void grid()
{
fstream infile("grid11.dat",ios::out);
for(r=11;r<=60;r++)
{
for(th=0; th<=2*pi; th=th+((pi/100)+(.00025*pi/98)))
{
cout<
|
|
|
|
|
Nileshb111 wrote: but this program is not running Once again you are expecting us to guess what is happening; we cannot see your screen or read your mind. Please edit your message, add <pre> tags around your code to make it readable, and explain exactly what the problem is and where it occurs.
|
|
|
|
|
I wanted to calculate outgoing flux on 2D circular grid. So first part of this code is for to get grid point after that i initializes variables on those grid points, now i want to calculate diagonal to get unit normal through it .
above code running upto global time step
|
|
|
|
|
please can u help me to solve posted problem
|
|
|
|
|
Not without some proper detail of the code you have written, what it is supposed to do, and what errors you receive.
|
|
|
|
|
Can u give a c++ code to calculate flux over 2D grids please
|
|
|
|
|
Nileshb111 wrote: Can u give a c++ code to calculate flux over 2D grids please Sorry, no, particularly as I have no idea what you mean by "calculate flux over 2D grids".
|
|
|
|
|