Hi,

I am trying to create a simple c-language function for "PostgreSQL
8.4.4, compiled by Visual C++ build 1400, 32-bit" running on Windows 7
(32-bit). It works, until I use a SPI.

1) CRASH: I successfully execute a simple query using SPI_exec(), but
when getting the result, it crashes:
SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
I tried to look wherever I could, but I have no idea what could go wrong.

2) NOT COMPILE: When I what to read value of global SPI_result, I
cannot compile - unresolved external symbol "_SPI_result". I am
currently linking with 'postgres.lib" and I haven't found anything
else what to link with additionally. Compiling using Visual Studio
2008, C/C++ project set to compile for "C".

Can anybody point out, what I am doing wrong?
The complete code and project are below.

Thanks, Tomas

======================== CODE =============
#define BUILDING_DLL 1

#include "postgres.h"
#include "fmgr.h"
#include "executor/spi.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

PG_FUNCTION_INFO_V1(crash_repro);


__declspec (dllexport)
Datum crash_repro(PG_FUNCTION_ARGS)
{
        int exec_result;

        SPI_connect();

        exec_result = SPI_exec("select 'test'", 0);

        elog(NOTICE, "exect_result_state = %i", exec_result);
        elog(NOTICE, "SPI_processed = %u", SPI_processed);

        if ((SPI_processed > 0) && (SPI_tuptable != NULL))
        {
                elog(NOTICE, "SPI_tuptable is not NULL");
                SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
        }

        SPI_finish();

        PG_RETURN_NULL();
}

===================== VISUAL STUDIO PROJECT ===============
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
        ProjectType="Visual C++"
        Version="9.00"
        Name="Crash"
        ProjectGUID="{39593D6A-2797-4680-8C97-88008C640B2D}"
        RootNamespace="Crash"
        Keyword="Win32Proj"
        TargetFrameworkVersion="196613"
        >
        <Platforms>
                <Platform
                        Name="Win32"
                />
        </Platforms>
        <ToolFiles>
        </ToolFiles>
        <Configurations>
                <Configuration
                        Name="Debug|Win32"
                        OutputDirectory="$(SolutionDir)$(ConfigurationName)"
                        IntermediateDirectory="$(ConfigurationName)"
                        ConfigurationType="2"
                        CharacterSet="1"
                        >
                        <Tool
                                Name="VCPreBuildEventTool"
                        />
                        <Tool
                                Name="VCCustomBuildTool"
                        />
                        <Tool
                                Name="VCXMLDataGeneratorTool"
                        />
                        <Tool
                                Name="VCWebServiceProxyGeneratorTool"
                        />
                        <Tool
                                Name="VCMIDLTool"
                        />
                        <Tool
                                Name="VCCLCompilerTool"
                                Optimization="0"
                                
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL"
                                MinimalRebuild="true"
                                BasicRuntimeChecks="3"
                                RuntimeLibrary="3"
                                UsePrecompiledHeader="0"
                                WarningLevel="3"
                                DebugInformationFormat="4"
                                CompileAs="1"
                        />
                        <Tool
                                Name="VCManagedResourceCompilerTool"
                        />
                        <Tool
                                Name="VCResourceCompilerTool"
                        />
                        <Tool
                                Name="VCPreLinkEventTool"
                        />
                        <Tool
                                Name="VCLinkerTool"
                                AdditionalDependencies="&quot;c:\Program
Files\PostgreSQL\8.4\lib\postgres.lib&quot;"
                                LinkIncremental="2"
                                GenerateDebugInformation="true"
                                SubSystem="2"
                                TargetMachine="1"
                        />
                        <Tool
                                Name="VCALinkTool"
                        />
                        <Tool
                                Name="VCManifestTool"
                        />
                        <Tool
                                Name="VCXDCMakeTool"
                        />
                        <Tool
                                Name="VCBscMakeTool"
                        />
                        <Tool
                                Name="VCFxCopTool"
                        />
                        <Tool
                                Name="VCAppVerifierTool"
                        />
                        <Tool
                                Name="VCPostBuildEventTool"
                        />
                </Configuration>
                <Configuration
                        Name="Release|Win32"
                        OutputDirectory="$(SolutionDir)$(ConfigurationName)"
                        IntermediateDirectory="$(ConfigurationName)"
                        ConfigurationType="2"
                        CharacterSet="1"
                        WholeProgramOptimization="1"
                        >
                        <Tool
                                Name="VCPreBuildEventTool"
                        />
                        <Tool
                                Name="VCCustomBuildTool"
                        />
                        <Tool
                                Name="VCXMLDataGeneratorTool"
                        />
                        <Tool
                                Name="VCWebServiceProxyGeneratorTool"
                        />
                        <Tool
                                Name="VCMIDLTool"
                        />
                        <Tool
                                Name="VCCLCompilerTool"
                                Optimization="2"
                                EnableIntrinsicFunctions="true"
                                
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;CRASH_EXPORTS"
                                RuntimeLibrary="2"
                                EnableFunctionLevelLinking="true"
                                UsePrecompiledHeader="2"
                                WarningLevel="3"
                                DebugInformationFormat="3"
                        />
                        <Tool
                                Name="VCManagedResourceCompilerTool"
                        />
                        <Tool
                                Name="VCResourceCompilerTool"
                        />
                        <Tool
                                Name="VCPreLinkEventTool"
                        />
                        <Tool
                                Name="VCLinkerTool"
                                LinkIncremental="1"
                                GenerateDebugInformation="true"
                                SubSystem="2"
                                OptimizeReferences="2"
                                EnableCOMDATFolding="2"
                                TargetMachine="1"
                        />
                        <Tool
                                Name="VCALinkTool"
                        />
                        <Tool
                                Name="VCManifestTool"
                        />
                        <Tool
                                Name="VCXDCMakeTool"
                        />
                        <Tool
                                Name="VCBscMakeTool"
                        />
                        <Tool
                                Name="VCFxCopTool"
                        />
                        <Tool
                                Name="VCAppVerifierTool"
                        />
                        <Tool
                                Name="VCPostBuildEventTool"
                        />
                </Configuration>
        </Configurations>
        <References>
        </References>
        <Files>
                <Filter
                        Name="Source Files"
                        Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
                        
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
                        >
                        <File
                                RelativePath=".\Crash.cpp"
                                >
                        </File>
                </Filter>
                <Filter
                        Name="Header Files"
                        Filter="h;hpp;hxx;hm;inl;inc;xsd"
                        
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
                        >
                </Filter>
                <Filter
                        Name="Resource Files"
                        
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
                        
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
                        >
                </Filter>
        </Files>
        <Globals>
        </Globals>
</VisualStudioProject>

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Reply via email to