The reason being is that you have not declared a size for your buffer. Therefore, the RegEnumValue function will be writing to memory which has not been allocated for it, and could possibly lead to stack corruption. Allocate the buffer to whatever size is required, i.e.
BYTE data[valueSize]; and in your function call dont pass the address of the ptr but just pass the ptr.
valueResult=RegEnumValue(hsubkey, i, valueName, &valueNameSize, NULL, &type, data,
&dataSize);
Hope this helps a bit, btw you probably might not need a buffer that size or declared locally on the statck. But i haven't really had a proper look at that reg function.