CGI - Problemas recoger variable string de C++ en CGI

 
Vista:
sin imagen de perfil

Problemas recoger variable string de C++ en CGI

Publicado por Raul (2 intervenciones) el 20/05/2016 11:27:43
Buenas,

estoy tratando de a partir de un archivo html que recoge el titulo de un disco pasarlo por get y al recogerlo en el archivo cgi con sscanf pasarlo a una variable y asi despues poder ejecutar una sentencia SQL para eliminar la fila que corresponde a ese titulo del album

No se si en sscanf debo formatear el titulo pasado por get.

El error que me lanza cuando envio los datos desde el html antes de abrirse el .cgi en el navegador me salta el error:
la instruccion "0x1f3d38" referenciada a la memoria "0xccccccc" la memoria no puede ser leida

* con variables de tipo integer no me ha dado problemas el programa ejecutado de forma similar, pero con variables string no hay forma.

Paso el codigo. Gracias :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <windows.h>
#include <sqlext.h>
#include <stdio.h>
 
main()
{
	SQLHENV henv1;
	SQLHDBC hdbc1;
	SQLHSTMT hstmt1;
	SQLRETURN estado;
	SQLCHAR* sentencia;
 
 
	SQLCHAR V_TITLE[20];
	SQLINTEGER V_TITLEInd=SQL_NTS;
 
 
 
	char *data;
 
	SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv1);
	SQLSetEnvAttr(henv1, SQL_ATTR_ODBC_VERSION,(SQLPOINTER*)SQL_OV_ODBC3, 0);
	SQLAllocHandle(SQL_HANDLE_DBC, henv1, &hdbc1);
	estado=SQLConnect(hdbc1, (SQLCHAR*)"conexion_ODBC", 20,
		(SQLCHAR*) "polly", 5,
		(SQLCHAR*) "gone", 4);
 
 
	printf("Content-type: text/html\n\n");
	printf("<HTML><body>\n");
 
	sentencia="DELETE FROM discs WHERE title=?";
 
	data=getenv("QUERY_STRING");
 
	if(data==NULL)
		printf("Error al pasar los datos\n");
	else if(sscanf(data,"title=%s",V_TITLE))
 
 
 
	SQLAllocHandle(SQL_HANDLE_STMT,hdbc1,&hstmt1);
 
 
	SQLBindParameter(hstmt1, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, SQL_NTS,    &V_TITLE, 0, &V_TITLEInd);
 
 
	estado=SQLExecDirect (hstmt1,sentencia,255);
 
 
 
	if(estado==SQL_SUCCESS || estado==SQL_SUCCESS_WITH_INFO)
	{
		printf("the delated has been executed successfully\n");
	}
	else{printf("deleting failed\n");}
 
 
	SQLEndTran(SQL_HANDLE_DBC,hdbc1, SQL_COMMIT);
	SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
	SQLDisconnect(hdbc1);
	SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
	SQLFreeHandle(SQL_HANDLE_ENV, henv1);
 
	printf("</body></HTML>\n");
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Problemas recoger variable string de C++ en CGI

Publicado por Raul (2 intervenciones) el 20/05/2016 15:11:07
He encontrado la forma de decodificar la variable pasada por get.
Pero Me da un error en cuanto llamo a la funciona decod, me indica que los parametros pasados no coinciden. Porque estoy pasando la direccion de memoria cuando en la funcion esta declarado que pase el valor de la variable. Pero veo que tiene logica como esta definido.
De todas formas he tratado de poner esas dos variables como direccion de memoria y tambien da error(bueno aun mas errores).

Si me podeis echar un cable a ver donde esta el fallo, estaria muy agradecido :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <windows.h>
#include <sqlext.h>
#include <stdio.h>
 
main()
{
 
	SQLHENV henv1;
	SQLHDBC hdbc1;
	SQLHSTMT hstmt1;
	SQLRETURN estado;
	SQLCHAR* sentencia;
 
	CHAR title[30];
	SQLCHAR V_TITLE[30];
	SQLINTEGER V_TITLEInd=SQL_NTS;
 
	char entdeco[30];
	long length;
	char* string_length;
 
	SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv1);
	SQLSetEnvAttr(henv1, SQL_ATTR_ODBC_VERSION,(SQLPOINTER*)SQL_OV_ODBC3, 0);
	SQLAllocHandle(SQL_HANDLE_DBC, henv1, &hdbc1);
	estado=SQLConnect(hdbc1, (SQLCHAR*)"conexion_ODBC", 20,
		(SQLCHAR*) "polly", 5,
		(SQLCHAR*) "gone", 4);
 
 
	printf("Content-type: text/html\n\n");
	printf("<HTML><body>\n");
 
	sentencia="DELETE FROM discs WHERE title=?";
 
	string_length=getenv("QUERY_STRING");
 
 
 
	sscanf(string_length,"%ld",&length);
 
	if (fgets(title, length+1,stdin)!=NULL)
	{
		decod(&title[0],length+1,&entdeco[0]);
 
		printf("<p>the delated has been executed successfully</p>");
 
 
		SQLAllocHandle(SQL_HANDLE_STMT,hdbc1,&hstmt1);
 
 
		SQLBindParameter(hstmt1, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 30, SQL_NTS, &V_TITLE, 0, &V_TITLEInd);
 
 
		estado=SQLExecDirect (hstmt1,sentencia,255);
	}
 
 
	else{printf("<p>deleting failed</p>");}
 
 
	SQLEndTran(SQL_HANDLE_DBC,hdbc1, SQL_COMMIT);
	SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
	SQLDisconnect(hdbc1);
	SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
	SQLFreeHandle(SQL_HANDLE_ENV, henv1);
 
	printf("</body></HTML>\n");
 
}
 
void decod(char* origin, long length, char* destination)
{
	int i;
	int j;
	int code;
 
	j=0;
	for (i=0;i<length;i++)
	{
 
		if(origin[i]=='+')
			destination[j]=' ';
		else if (origin[i]=='%')
		{		if(sscanf(&origin[i+1],"%2x",&code)!=1)
					code='?';
				destination[j]=code;
				i+=2;
		}
		else
		destination[j]=origin[i];
 
		j++;
	}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar